Console_color2 helper.
If $console_color isn't instantiated, remove the color directives and print.
This commit is contained in:
Tony Murray
2016-08-16 19:52:36 -05:00
parent 4a152dab7b
commit aa8294bfce
6 changed files with 30 additions and 27 deletions

View File

@@ -182,7 +182,7 @@ if (!empty($argv[1])) {
}
} else {
print $console_color->convert(
c_echo(
"\n".$config['project_name_version'].' Add Host Tool
Usage (SNMPv1/2c): ./addhost.php [-g <poller group>] [-f] [-p <port assoc mode>] <%Whostname%n> [community] [v1|v2c] [port] ['.implode('|', $config['snmp']['transports']).']

View File

@@ -95,28 +95,18 @@ function isCli() {
}
}
function print_error($text, $quiet = false) {
if ($quiet) {
return;
}
function print_error($text) {
if (isCli()) {
global $console_color;
print $console_color->convert("%r".$text."%n\n", false);
c_echo("%r".$text."%n\n");
}
else {
echo('<div class="alert alert-danger"><img src="images/16/exclamation.png" align="absmiddle"> '.$text.'</div>');
}
}
function print_message($text, $quiet = false) {
if ($quiet) {
return;
}
function print_message($text) {
if (isCli()) {
global $console_color;
print $console_color->convert("%g".$text."%n\n", false);
c_echo("%g".$text."%n\n");
}
else {
echo('<div class="alert alert-success"><img src="images/16/tick.png" align="absmiddle"> '.$text.'</div>');
@@ -124,8 +114,6 @@ function print_message($text, $quiet = false) {
}
function delete_port($int_id) {
global $config;
$interface = dbFetchRow("SELECT * FROM `ports` AS P, `devices` AS D WHERE P.port_id = ? AND D.device_id = P.device_id", array($int_id));
$interface_tables = array('adjacencies', 'ipaddr', 'ip6adjacencies', 'ip6addr', 'mac_accounting', 'bill_ports', 'pseudowires', 'ports');
@@ -635,6 +623,22 @@ function d_echo($text, $no_debug_text = null) {
}
} // d_echo
/**
* Output using console color if possible
* https://github.com/pear/Console_Color2/blob/master/examples/documentation
*
* @param string $string the string to print with console color
*/
function c_echo($string)
{
global $console_color;
if($console_color) {
echo $console_color->convert($string);
} else {
echo preg_replace('/%((%)|.)/', '', $string);
}
}
/*
* convenience function - please use this instead of 'if ($debug) { print_r ...; }'

View File

@@ -24,7 +24,7 @@
function dbQuery($sql, $parameters=array()) {
global $fullSql, $debug, $sql_debug, $console_color;
global $fullSql, $debug, $sql_debug;
$fullSql = dbMakeQuery($sql, $parameters);
if ($debug) {
if (php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) {
@@ -32,7 +32,7 @@ function dbQuery($sql, $parameters=array()) {
echo "\nINSERT INTO `alert_log` entry masked due to binary data\n";
}
else {
print $console_color->convert("\nSQL[%y".$fullSql.'%n] ');
c_echo("\nSQL[%y".$fullSql.'%n] ');
}
}
else {

View File

@@ -24,7 +24,7 @@
function dbQuery($sql, $parameters=array()) {
global $fullSql, $debug, $sql_debug, $console_color, $database_link;
global $fullSql, $debug, $sql_debug, $database_link;
$fullSql = dbMakeQuery($sql, $parameters);
if ($debug) {
if (php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) {
@@ -32,7 +32,7 @@ function dbQuery($sql, $parameters=array()) {
echo "\nINSERT INTO `alert_log` entry masked due to binary data\n";
}
else {
print $console_color->convert("\nSQL[%y".$fullSql.'%n] ');
c_echo("\nSQL[%y".$fullSql.'%n] ');
}
}
else {

View File

@@ -46,7 +46,7 @@ function influxdb_connect() {
}// end influxdb_connect
function influx_update($device,$measurement,$tags=array(),$fields) {
global $influxdb,$config,$console_color;
global $influxdb, $config;
if ($influxdb !== false) {
$tmp_fields = array();
$tmp_tags['hostname'] = $device['hostname'];
@@ -87,7 +87,7 @@ function influx_update($device,$measurement,$tags=array(),$fields) {
}
}
else {
print $console_color->convert('[%gInfluxDB Disabled%n] ', false);
c_echo("[%gInfluxDB Disabled%n]\n");
}//end if
}//end if
}// end influx_update

View File

@@ -147,11 +147,10 @@ function rrdtool_graph($graph_file, $options)
* @global $config
* @global $debug
* @global $rrd_pipes
* @global $console_color
*/
function rrdtool($command, $filename, $options, $timeout = 0)
{
global $config, $debug, $rrd_pipes, $console_color;
global $config, $debug, $rrd_pipes;
// do not ovewrite files when creating
if ($command == 'create') {
@@ -177,7 +176,7 @@ function rrdtool($command, $filename, $options, $timeout = 0)
if ($config['norrd'] && !in_array($command,
array('graph', 'graphv', 'dump', 'fetch', 'first', 'last', 'lastupdate', 'info', 'xport'))
) {
print $console_color->convert('[%rRRD Disabled%n]');
c_echo('[%rRRD Disabled%n]');
$output = array(null, null);
} elseif ($command == 'create' &&
version_compare($config['rrdtool_version'], '1.5', '<') &&
@@ -199,7 +198,7 @@ function rrdtool($command, $filename, $options, $timeout = 0)
}
if ($debug) {
print $console_color->convert('RRD[%g'.$cmd."%n] \n");
c_echo('RRD[%g' . $cmd . "%n] \n");
echo 'RRDtool Output: ';
echo $output[0];
echo $output[1];