diff options
-rw-r--r-- | pjlib/include/pj/log.h | 53 | ||||
-rw-r--r-- | pjlib/src/pj/log.c | 86 | ||||
-rw-r--r-- | pjlib/src/pj/log_writer_stdout.c | 34 | ||||
-rw-r--r-- | pjlib/src/pj/os_core_unix.c | 53 | ||||
-rw-r--r-- | pjsip-apps/src/pjsua/pjsua_app.c | 20 | ||||
-rw-r--r-- | pjsip/src/pjsua-lib/pjsua_core.c | 8 |
6 files changed, 212 insertions, 42 deletions
diff --git a/pjlib/include/pj/log.h b/pjlib/include/pj/log.h index 76d609dd..2d0961c9 100644 --- a/pjlib/include/pj/log.h +++ b/pjlib/include/pj/log.h @@ -68,15 +68,17 @@ PJ_BEGIN_DECL */ enum pj_log_decoration { - PJ_LOG_HAS_DAY_NAME = 1, /**< Include day name [default: no]. */ - PJ_LOG_HAS_YEAR = 2, /**< Include year digit [default: no] */ - PJ_LOG_HAS_MONTH = 4, /**< Include month [default: no] */ - PJ_LOG_HAS_DAY_OF_MON = 8, /**< Include day of month [default: no] */ - PJ_LOG_HAS_TIME = 16, /**< Include time [default: yes]. */ - PJ_LOG_HAS_MICRO_SEC = 32, /**< Include microseconds [yes] */ - PJ_LOG_HAS_SENDER = 64, /**< Include sender in the log [yes]. */ - PJ_LOG_HAS_NEWLINE = 128, /**< Terminate each call with newline [yes].*/ - PJ_LOG_HAS_CR = 256 /**< Include carriage return [no]. */ + PJ_LOG_HAS_DAY_NAME = 1, /**< Include day name [default: no] */ + PJ_LOG_HAS_YEAR = 2, /**< Include year digit [no] */ + PJ_LOG_HAS_MONTH = 4, /**< Include month [no] */ + PJ_LOG_HAS_DAY_OF_MON = 8, /**< Include day of month [no] */ + PJ_LOG_HAS_TIME = 16, /**< Include time [yes] */ + PJ_LOG_HAS_MICRO_SEC = 32, /**< Include microseconds [yes] */ + PJ_LOG_HAS_SENDER = 64, /**< Include sender in the log [yes] */ + PJ_LOG_HAS_NEWLINE = 128, /**< Terminate each call with newline [yes] */ + PJ_LOG_HAS_CR = 256, /**< Include carriage return [no] */ + PJ_LOG_HAS_SPACE = 512, /**< Include two spaces before log [yes] */ + PJ_LOG_HAS_COLOR = 1024 /**< Colorize logs [yes on win32] */ }; /** @@ -199,6 +201,23 @@ PJ_DECL(void) pj_log_set_decor(unsigned decor); PJ_DECL(unsigned) pj_log_get_decor(void); +/** + * Set color of log messages. + * + * @param level Log level which color will be changed. + * @param color Desired color. + */ +PJ_DECL(void) pj_log_set_color(int level, pj_color_t color); + +/** + * Get color of log messages. + * + * @param level Log level which color will be returned. + * @return Log color. + */ +PJ_DECL(pj_color_t) pj_log_get_color(int level); + + #else /* #if PJ_LOG_MAX_LEVEL >= 1 */ /** @@ -236,6 +255,14 @@ PJ_DECL(unsigned) pj_log_get_decor(void); # define pj_log_set_decor(decor) /** + * Set color of log messages. + * + * @param level Log level which color will be changed. + * @param color Desired color. + */ +# define pj_log_set_color(level, color) + +/** * Get current maximum log verbositylevel. * * @return Current log maximum level. @@ -249,6 +276,14 @@ PJ_DECL(unsigned) pj_log_get_decor(void); */ # define pj_log_get_decor() 0 +/** + * Get color of log messages. + * + * @param level Log level which color will be returned. + * @return Log color. + */ +# define pj_log_get_color(level) 0 + #endif /* #if PJ_LOG_MAX_LEVEL >= 1 */ diff --git a/pjlib/src/pj/log.c b/pjlib/src/pj/log.c index 164e25d0..863d868d 100644 --- a/pjlib/src/pj/log.c +++ b/pjlib/src/pj/log.c @@ -31,7 +31,35 @@ static int pj_log_max_level = PJ_LOG_MAX_LEVEL; #endif static pj_log_func *log_writer = &pj_log_write; static unsigned log_decor = PJ_LOG_HAS_TIME | PJ_LOG_HAS_MICRO_SEC | - PJ_LOG_HAS_SENDER | PJ_LOG_HAS_NEWLINE; + PJ_LOG_HAS_SENDER | PJ_LOG_HAS_NEWLINE | + PJ_LOG_HAS_SPACE +#if defined(PJ_WIN32) && PJ_WIN32!=0 + | PJ_LOG_HAS_COLOR +#endif + ; + +static pj_color_t PJ_LOG_COLOR_0 = PJ_TERM_COLOR_BRIGHT | PJ_TERM_COLOR_R; +static pj_color_t PJ_LOG_COLOR_1 = PJ_TERM_COLOR_BRIGHT | PJ_TERM_COLOR_R; +static pj_color_t PJ_LOG_COLOR_2 = PJ_TERM_COLOR_BRIGHT | + PJ_TERM_COLOR_R | + PJ_TERM_COLOR_G; +static pj_color_t PJ_LOG_COLOR_3 = PJ_TERM_COLOR_BRIGHT | + PJ_TERM_COLOR_R | + PJ_TERM_COLOR_G | + PJ_TERM_COLOR_B; +static pj_color_t PJ_LOG_COLOR_4 = PJ_TERM_COLOR_R | + PJ_TERM_COLOR_G | + PJ_TERM_COLOR_B; +static pj_color_t PJ_LOG_COLOR_5 = PJ_TERM_COLOR_R | + PJ_TERM_COLOR_G | + PJ_TERM_COLOR_B; +static pj_color_t PJ_LOG_COLOR_6 = PJ_TERM_COLOR_R | + PJ_TERM_COLOR_G | + PJ_TERM_COLOR_B; +/* Default terminal color */ +static pj_color_t PJ_LOG_COLOR_77 = PJ_TERM_COLOR_R | + PJ_TERM_COLOR_G | + PJ_TERM_COLOR_B; #if PJ_LOG_USE_STACK_BUFFER==0 static char log_buffer[PJ_LOG_MAX_SIZE]; @@ -47,6 +75,56 @@ PJ_DEF(unsigned) pj_log_get_decor(void) return log_decor; } +PJ_DEF(void) pj_log_set_color(int level, pj_color_t color) +{ + switch (level) + { + case 0: PJ_LOG_COLOR_0 = color; + break; + case 1: PJ_LOG_COLOR_1 = color; + break; + case 2: PJ_LOG_COLOR_2 = color; + break; + case 3: PJ_LOG_COLOR_3 = color; + break; + case 4: PJ_LOG_COLOR_4 = color; + break; + case 5: PJ_LOG_COLOR_5 = color; + break; + case 6: PJ_LOG_COLOR_6 = color; + break; + /* Default terminal color */ + case 77: PJ_LOG_COLOR_77 = color; + break; + default: + /* Do nothing */ + break; + } +} + +PJ_DEF(pj_color_t) pj_log_get_color(int level) +{ + switch (level) { + case 0: + return PJ_LOG_COLOR_0; + case 1: + return PJ_LOG_COLOR_1; + case 2: + return PJ_LOG_COLOR_2; + case 3: + return PJ_LOG_COLOR_3; + case 4: + return PJ_LOG_COLOR_4; + case 5: + return PJ_LOG_COLOR_5; + case 6: + return PJ_LOG_COLOR_6; + default: + /* Return default terminal color */ + return PJ_LOG_COLOR_77; + } +} + PJ_DEF(void) pj_log_set_level(int level) { pj_log_max_level = level; @@ -105,7 +183,7 @@ PJ_DEF(void) pj_log( const char *sender, int level, pre += pj_utoa_pad(ptime.mon+1, pre, 2, '0'); } if (log_decor & PJ_LOG_HAS_DAY_OF_MON) { - *pre++ = ' '; + *pre++ = '-'; pre += pj_utoa_pad(ptime.day, pre, 2, '0'); } if (log_decor & PJ_LOG_HAS_TIME) { @@ -139,6 +217,10 @@ PJ_DEF(void) pj_log( const char *sender, int level, if (log_decor != 0 && log_decor != PJ_LOG_HAS_NEWLINE) *pre++ = ' '; + if (log_decor & PJ_LOG_HAS_SPACE) { + *pre++ = ' '; + } + len = pre - log_buffer; /* Print the whole message to the string log_buffer. */ diff --git a/pjlib/src/pj/log_writer_stdout.c b/pjlib/src/pj/log_writer_stdout.c index 9bffe466..e14b2dfa 100644 --- a/pjlib/src/pj/log_writer_stdout.c +++ b/pjlib/src/pj/log_writer_stdout.c @@ -20,37 +20,19 @@ #include <pj/os.h> #include <pj/compat/stdfileio.h> -#define CLR_FATAL (PJ_TERM_COLOR_BRIGHT | PJ_TERM_COLOR_R) -#define CLR_WARNING (PJ_TERM_COLOR_BRIGHT | PJ_TERM_COLOR_R | PJ_TERM_COLOR_G) -#define CLR_INFO (PJ_TERM_COLOR_BRIGHT | PJ_TERM_COLOR_R | PJ_TERM_COLOR_G | \ - PJ_TERM_COLOR_B) -#define CLR_DEFAULT (PJ_TERM_COLOR_R | PJ_TERM_COLOR_G | PJ_TERM_COLOR_B) static void term_set_color(int level) { #if defined(PJ_TERM_HAS_COLOR) && PJ_TERM_HAS_COLOR != 0 - unsigned attr = 0; - switch (level) { - case 0: - case 1: attr = CLR_FATAL; - break; - case 2: attr = CLR_WARNING; - break; - case 3: attr = CLR_INFO; - break; - default: - attr = CLR_DEFAULT; - break; - } - - pj_term_set_color(attr); + pj_term_set_color(pj_log_get_color(level)); #endif } static void term_restore_color(void) { #if defined(PJ_TERM_HAS_COLOR) && PJ_TERM_HAS_COLOR != 0 - pj_term_set_color(CLR_DEFAULT); + /* Set terminal to its default color */ + pj_term_set_color(pj_log_get_color(77)); #endif } @@ -61,8 +43,12 @@ PJ_DEF(void) pj_log_write(int level, const char *buffer, int len) PJ_UNUSED_ARG(len); /* Copy to terminal/file. */ - term_set_color(level); - printf("%s", buffer); - term_restore_color(); + if (pj_log_get_decor() & PJ_LOG_HAS_COLOR) { + term_set_color(level); + printf("%s", buffer); + term_restore_color(); + } else { + printf("%s", buffer); + } } diff --git a/pjlib/src/pj/os_core_unix.c b/pjlib/src/pj/os_core_unix.c index d6ce78ed..006f2a0b 100644 --- a/pjlib/src/pj/os_core_unix.c +++ b/pjlib/src/pj/os_core_unix.c @@ -1689,8 +1689,57 @@ PJ_DEF(pj_status_t) pj_event_destroy(pj_event_t *event) */ PJ_DEF(pj_status_t) pj_term_set_color(pj_color_t color) { - PJ_UNUSED_ARG(color); - return PJ_EINVALIDOP; + /* put bright prefix to ansi_color */ + char ansi_color[12] = "\033[01;3"; + + if (color & PJ_TERM_COLOR_BRIGHT) { + color ^= PJ_TERM_COLOR_BRIGHT; + } else { + strcpy(ansi_color, "\033[00;3"); + } + + switch (color) { + case 0: + /* black color */ + strcat(ansi_color, "0m"); + break; + case PJ_TERM_COLOR_R: + /* red color */ + strcat(ansi_color, "1m"); + break; + case PJ_TERM_COLOR_G: + /* green color */ + strcat(ansi_color, "2m"); + break; + case PJ_TERM_COLOR_B: + /* blue color */ + strcat(ansi_color, "4m"); + break; + case PJ_TERM_COLOR_R | PJ_TERM_COLOR_G: + /* yellow color */ + strcat(ansi_color, "3m"); + break; + case PJ_TERM_COLOR_R | PJ_TERM_COLOR_B: + /* magenta color */ + strcat(ansi_color, "5m"); + break; + case PJ_TERM_COLOR_G | PJ_TERM_COLOR_B: + /* cyan color */ + strcat(ansi_color, "6m"); + break; + case PJ_TERM_COLOR_R | PJ_TERM_COLOR_G | PJ_TERM_COLOR_B: + /* white color */ + strcat(ansi_color, "7m"); + break; + default: + /* default console color */ + strcpy(ansi_color, "\033[00m"); + break; + } + + fputs(ansi_color, stdout); + + return PJ_SUCCESS; } /** diff --git a/pjsip-apps/src/pjsua/pjsua_app.c b/pjsip-apps/src/pjsua/pjsua_app.c index 8b28b50d..b8ca056f 100644 --- a/pjsip-apps/src/pjsua/pjsua_app.c +++ b/pjsip-apps/src/pjsua/pjsua_app.c @@ -158,6 +158,9 @@ static void usage(void) puts (" --log-file=fname Log to filename (default stderr)"); puts (" --log-level=N Set log max level to N (0(none) to 6(trace)) (default=5)"); puts (" --app-log-level=N Set log max level for stdout display (default=4)"); + puts (" --color Use colorful logging (default yes on Win32)"); + puts (" --no-color Disable colorful logging"); + puts (""); puts ("SIP Account options:"); puts (" --use-ims Enable 3GPP/IMS related settings on this account"); @@ -442,6 +445,7 @@ static pj_status_t parse_args(int argc, char *argv[], int c; int option_index; enum { OPT_CONFIG_FILE=127, OPT_LOG_FILE, OPT_LOG_LEVEL, OPT_APP_LOG_LEVEL, + OPT_COLOR, OPT_NO_COLOR, OPT_HELP, OPT_VERSION, OPT_NULL_AUDIO, OPT_SND_AUTO_CLOSE, OPT_LOCAL_PORT, OPT_IP_ADDR, OPT_PROXY, OPT_OUTBOUND_PROXY, OPT_REGISTRAR, OPT_REG_TIMEOUT, OPT_PUBLISH, OPT_ID, OPT_CONTACT, @@ -476,6 +480,8 @@ static pj_status_t parse_args(int argc, char *argv[], { "log-file", 1, 0, OPT_LOG_FILE}, { "log-level", 1, 0, OPT_LOG_LEVEL}, { "app-log-level",1,0,OPT_APP_LOG_LEVEL}, + { "color", 0, 0, OPT_COLOR}, + { "no-color", 0, 0, OPT_NO_COLOR}, { "help", 0, 0, OPT_HELP}, { "version", 0, 0, OPT_VERSION}, { "clock-rate", 1, 0, OPT_CLOCK_RATE}, @@ -637,6 +643,14 @@ static pj_status_t parse_args(int argc, char *argv[], } break; + case OPT_COLOR: + cfg->log_cfg.decor |= PJ_LOG_HAS_COLOR; + break; + + case OPT_NO_COLOR: + cfg->log_cfg.decor &= ~PJ_LOG_HAS_COLOR; + break; + case OPT_HELP: usage(); return PJ_EINVAL; @@ -3756,7 +3770,7 @@ void console_app_main(const pj_str_t *uri_to_call) len = write_settings(&app_config, settings, sizeof(settings)); if (len < 1) - PJ_LOG(3,(THIS_FILE, "Error: not enough buffer")); + PJ_LOG(1,(THIS_FILE, "Error: not enough buffer")); else PJ_LOG(3,(THIS_FILE, "Dumping configuration (%d bytes):\n%s\n", @@ -3783,7 +3797,7 @@ void console_app_main(const pj_str_t *uri_to_call) len = write_settings(&app_config, settings, sizeof(settings)); if (len < 1) - PJ_LOG(3,(THIS_FILE, "Error: not enough buffer")); + PJ_LOG(1,(THIS_FILE, "Error: not enough buffer")); else { pj_oshandle_t fd; pj_status_t status; @@ -4081,7 +4095,7 @@ pj_status_t app_init(int argc, char *argv[]) #endif if (transport_id == -1) { - PJ_LOG(3,(THIS_FILE, "Error: no transport is configured")); + PJ_LOG(1,(THIS_FILE, "Error: no transport is configured")); status = -1; goto on_error; } diff --git a/pjsip/src/pjsua-lib/pjsua_core.c b/pjsip/src/pjsua-lib/pjsua_core.c index bd21a4b8..fe95348c 100644 --- a/pjsip/src/pjsua-lib/pjsua_core.c +++ b/pjsip/src/pjsua-lib/pjsua_core.c @@ -40,7 +40,7 @@ PJ_DEF(void) pjsua_perror( const char *sender, const char *title, char errmsg[PJ_ERR_MSG_SIZE]; pj_strerror(status, errmsg, sizeof(errmsg)); - PJ_LOG(3,(sender, "%s: %s [status=%d]", title, errmsg, status)); + PJ_LOG(1,(sender, "%s: %s [status=%d]", title, errmsg, status)); } @@ -69,7 +69,11 @@ PJ_DEF(void) pjsua_logging_config_default(pjsua_logging_config *cfg) cfg->level = 5; cfg->console_level = 4; cfg->decor = PJ_LOG_HAS_SENDER | PJ_LOG_HAS_TIME | - PJ_LOG_HAS_MICRO_SEC | PJ_LOG_HAS_NEWLINE; + PJ_LOG_HAS_MICRO_SEC | PJ_LOG_HAS_NEWLINE | + PJ_LOG_HAS_SPACE; +#if defined(PJ_WIN32) && PJ_WIN32 != 0 + cfg->decor |= PJ_LOG_HAS_COLOR; +#endif } PJ_DEF(void) pjsua_logging_config_dup(pj_pool_t *pool, |