diff --git a/potx.inc b/potx.inc index 8a4b2a4..14d4824 100644 --- a/potx.inc +++ b/potx.inc @@ -297,7 +297,7 @@ function _potx_process_file($file_path, $strip_prefix = 0, $save_callback = '_po _potx_find_t_calls($file_name, $save_callback, 'critical'); _potx_find_t_calls($file_name, $save_callback, 'alert'); _potx_find_t_calls($file_name, $save_callback, 'emergency'); - // @todo: add support for log() + _potx_find_log_calls($file_name, $save_callback); } } else { @@ -954,6 +954,54 @@ function _potx_find_watchdog_calls($file, $save_callback) { } /** + * Detect all occurances of log() calls. + * + * These sequences are searched for: + * log + "(" + ..anything (might be more tokens).. + + * "," + T_CONSTANT_ENCAPSED_STRING + whatever. + * + * @param $file + * Name of file parsed. + * @param $save_callback + * Callback function used to save strings. + */ +function _potx_find_log_calls($file, $save_callback) { + global $_potx_tokens, $_potx_lookup; + + if (isset($_potx_lookup['log'])) { + foreach ($_potx_lookup['log'] as $ti) { + list($ctok, $par1) = array($_potx_tokens[$ti], $_potx_tokens[$ti+1]); + list($type, $string, $line) = $ctok; + if ($par1 == "(") { + // Eat up everything that is used as the first parameter + $tn = $ti + 2; + $depth = 0; + while (!($_potx_tokens[$tn] == "," && $depth == 0)) { + if ($_potx_tokens[$tn] == "(") { + $depth++; + } + elseif ($_potx_tokens[$tn] == ")") { + $depth--; + } + $tn++; + } + // Get further parameters + list($comma, $message, $par2) = array($_potx_tokens[$tn], $_potx_tokens[$tn+1], $_potx_tokens[$tn+2]); + if (($comma == ',') && in_array($par2, array(')', ',')) && + (is_array($message) && ($message[0] == T_CONSTANT_ENCAPSED_STRING))) { + // Context is not supported on watchdog(). + $save_callback(_potx_format_quoted_string($message[1]), POTX_CONTEXT_NONE, $file, $line); + } + else { + // log() found, but the parameters are not correct. + _potx_marker_error($file, $line, 'log', $ti, t('In log(), the log level should be followed by a literal string. There should be no variables, concatenation, constants or even a t() call there.'), 'http://drupal.org/node/323101'); + } + } + } + } +} + +/** * Detect all occurances of format_plural calls. * * These sequences are searched for: diff --git a/tests/potx.test b/tests/potx.test index bf625e4..9699ccf 100644 --- a/tests/potx.test +++ b/tests/potx.test @@ -270,6 +270,9 @@ class PotxTestCase extends DrupalWebTestCase { $this->assertMsgID('Critical message'); $this->assertMsgID('Alert message'); $this->assertMsgID('Emergency message'); + $this->assertMsgID('Log message'); + $this->assertMsgID('Log message 2'); + $this->assertMsgID('Log message 3'); } /** diff --git a/tests/potx_test_8.module b/tests/potx_test_8.module index 9626332..def9a05 100644 --- a/tests/potx_test_8.module +++ b/tests/potx_test_8.module @@ -24,6 +24,9 @@ function potx_test_8_example() { $f->critical('Critical message'); $g->alert('Alert message'); $h->emergency('Emergency message'); + $i->log(1, 'Log message'); + $i->log($boo, 'Log message 2'); + $i->log(potx_test_8_sample(), 'Log message 3'); } /**