Ticket #571: runtime-color.patch
File runtime-color.patch, 10.6 KB (added by Ondrej.Sterbak, 16 years ago) |
---|
-
pjsip/src/pjsua-lib/pjsua_core.c
40 40 char errmsg[PJ_ERR_MSG_SIZE]; 41 41 42 42 pj_strerror(status, errmsg, sizeof(errmsg)); 43 PJ_LOG( 3,(sender, "%s: %s [status=%d]", title, errmsg, status));43 PJ_LOG(1,(sender, "%s: %s [status=%d]", title, errmsg, status)); 44 44 } 45 45 46 46 … … 69 69 cfg->level = 5; 70 70 cfg->console_level = 4; 71 71 cfg->decor = PJ_LOG_HAS_SENDER | PJ_LOG_HAS_TIME | 72 PJ_LOG_HAS_MICRO_SEC | PJ_LOG_HAS_NEWLINE; 72 PJ_LOG_HAS_MICRO_SEC | PJ_LOG_HAS_NEWLINE | 73 PJ_LOG_HAS_SPACE | PJ_LOG_HAS_COLOR; 73 74 } 74 75 75 76 PJ_DEF(void) pjsua_logging_config_dup(pj_pool_t *pool, -
pjlib/include/pj/log.h
68 68 */ 69 69 enum pj_log_decoration 70 70 { 71 PJ_LOG_HAS_DAY_NAME = 1, /**< Include day name [default: no]. */ 72 PJ_LOG_HAS_YEAR = 2, /**< Include year digit [default: no] */ 73 PJ_LOG_HAS_MONTH = 4, /**< Include month [default: no] */ 74 PJ_LOG_HAS_DAY_OF_MON = 8, /**< Include day of month [default: no] */ 75 PJ_LOG_HAS_TIME = 16, /**< Include time [default: yes]. */ 76 PJ_LOG_HAS_MICRO_SEC = 32, /**< Include microseconds [yes] */ 77 PJ_LOG_HAS_SENDER = 64, /**< Include sender in the log [yes]. */ 78 PJ_LOG_HAS_NEWLINE = 128, /**< Terminate each call with newline [yes].*/ 79 PJ_LOG_HAS_CR = 256 /**< Include carriage return [no]. */ 71 PJ_LOG_HAS_DAY_NAME = 1, /**< Include day name [default: no] */ 72 PJ_LOG_HAS_YEAR = 2, /**< Include year digit [no] */ 73 PJ_LOG_HAS_MONTH = 4, /**< Include month [no] */ 74 PJ_LOG_HAS_DAY_OF_MON = 8, /**< Include day of month [no] */ 75 PJ_LOG_HAS_TIME = 16, /**< Include time [yes] */ 76 PJ_LOG_HAS_MICRO_SEC = 32, /**< Include microseconds [yes] */ 77 PJ_LOG_HAS_SENDER = 64, /**< Include sender in the log [yes] */ 78 PJ_LOG_HAS_NEWLINE = 128, /**< Terminate each call with newline [yes] */ 79 PJ_LOG_HAS_CR = 256, /**< Include carriage return [no] */ 80 PJ_LOG_HAS_SPACE = 512, /**< Include two spaces before log [yes] */ 81 PJ_LOG_HAS_COLOR = 1024 /**< Colorize logs [yes] */ 80 82 }; 81 83 82 84 /** … … 199 201 PJ_DECL(unsigned) pj_log_get_decor(void); 200 202 201 203 204 /** 205 * Set color of log messages. 206 * 207 * @param level Log level which color will be changed. 208 * @param color Desired color. 209 */ 210 PJ_DECL(void) pj_log_set_color(int level, pj_color_t color); 211 212 /** 213 * Get color of log messages. 214 * 215 * @param level Log level which color will be returned. 216 * @return Log color. 217 */ 218 PJ_DECL(pj_color_t) pj_log_get_color(int level); 219 220 202 221 #else /* #if PJ_LOG_MAX_LEVEL >= 1 */ 203 222 204 223 /** … … 236 255 # define pj_log_set_decor(decor) 237 256 238 257 /** 258 * Set color of log messages. 259 * 260 * @param level Log level which color will be changed. 261 * @param color Desired color. 262 */ 263 # define pj_log_set_color(level, color) 264 265 /** 239 266 * Get current maximum log verbositylevel. 240 267 * 241 268 * @return Current log maximum level. … … 249 276 */ 250 277 # define pj_log_get_decor() 0 251 278 279 /** 280 * Get color of log messages. 281 * 282 * @param level Log level which color will be returned. 283 * @return Log color. 284 */ 285 # define pj_log_get_color(level) 0 286 252 287 253 288 #endif /* #if PJ_LOG_MAX_LEVEL >= 1 */ 254 289 -
pjlib/src/pj/log_writer_stdout.c
20 20 #include <pj/os.h> 21 21 #include <pj/compat/stdfileio.h> 22 22 23 #define CLR_FATAL (PJ_TERM_COLOR_BRIGHT | PJ_TERM_COLOR_R)24 #define CLR_WARNING (PJ_TERM_COLOR_BRIGHT | PJ_TERM_COLOR_R | PJ_TERM_COLOR_G)25 #define CLR_INFO (PJ_TERM_COLOR_BRIGHT | PJ_TERM_COLOR_R | PJ_TERM_COLOR_G | \26 PJ_TERM_COLOR_B)27 #define CLR_DEFAULT (PJ_TERM_COLOR_R | PJ_TERM_COLOR_G | PJ_TERM_COLOR_B)28 23 29 24 static void term_set_color(int level) 30 25 { 31 26 #if defined(PJ_TERM_HAS_COLOR) && PJ_TERM_HAS_COLOR != 0 32 unsigned attr = 0; 33 switch (level) { 34 case 0: 35 case 1: attr = CLR_FATAL; 36 break; 37 case 2: attr = CLR_WARNING; 38 break; 39 case 3: attr = CLR_INFO; 40 break; 41 default: 42 attr = CLR_DEFAULT; 43 break; 44 } 45 46 pj_term_set_color(attr); 27 pj_term_set_color(pj_log_get_color(level)); 47 28 #endif 48 29 } 49 30 50 31 static void term_restore_color(void) 51 32 { 52 33 #if defined(PJ_TERM_HAS_COLOR) && PJ_TERM_HAS_COLOR != 0 53 pj_term_set_color(CLR_DEFAULT); 34 /* Set terminal to its default color */ 35 pj_term_set_color(pj_log_get_color(77)); 54 36 #endif 55 37 } 56 38 … … 61 43 PJ_UNUSED_ARG(len); 62 44 63 45 /* Copy to terminal/file. */ 64 term_set_color(level); 65 printf("%s", buffer); 66 term_restore_color(); 46 if (pj_log_get_decor() & PJ_LOG_HAS_COLOR) { 47 term_set_color(level); 48 printf("%s", buffer); 49 term_restore_color(); 50 } else { 51 printf("%s", buffer); 52 } 67 53 } 68 54 -
pjlib/src/pj/os_core_unix.c
1689 1689 */ 1690 1690 PJ_DEF(pj_status_t) pj_term_set_color(pj_color_t color) 1691 1691 { 1692 PJ_UNUSED_ARG(color); 1693 return PJ_EINVALIDOP; 1692 /* put bright prefix to ansi_color */ 1693 char ansi_color[12] = "\033[01;3"; 1694 1695 if (color & PJ_TERM_COLOR_BRIGHT) { 1696 color ^= PJ_TERM_COLOR_BRIGHT; 1697 } else { 1698 strcpy(ansi_color, "\033[00;3"); 1699 } 1700 1701 switch (color) { 1702 case 0: 1703 /* black color */ 1704 strcat(ansi_color, "0m"); 1705 break; 1706 case PJ_TERM_COLOR_R: 1707 /* red color */ 1708 strcat(ansi_color, "1m"); 1709 break; 1710 case PJ_TERM_COLOR_G: 1711 /* green color */ 1712 strcat(ansi_color, "2m"); 1713 break; 1714 case PJ_TERM_COLOR_B: 1715 /* blue color */ 1716 strcat(ansi_color, "4m"); 1717 break; 1718 case PJ_TERM_COLOR_R | PJ_TERM_COLOR_G: 1719 /* yellow color */ 1720 strcat(ansi_color, "3m"); 1721 break; 1722 case PJ_TERM_COLOR_R | PJ_TERM_COLOR_B: 1723 /* magenta color */ 1724 strcat(ansi_color, "5m"); 1725 break; 1726 case PJ_TERM_COLOR_G | PJ_TERM_COLOR_B: 1727 /* cyan color */ 1728 strcat(ansi_color, "6m"); 1729 break; 1730 case PJ_TERM_COLOR_R | PJ_TERM_COLOR_G | PJ_TERM_COLOR_B: 1731 /* white color */ 1732 strcat(ansi_color, "7m"); 1733 break; 1734 default: 1735 /* default console color */ 1736 strcpy(ansi_color, "\033[00m"); 1737 break; 1738 } 1739 1740 fputs(ansi_color, stdout); 1741 1742 return PJ_SUCCESS; 1694 1743 } 1695 1744 1696 1745 /** -
pjlib/src/pj/log.c
31 31 #endif 32 32 static pj_log_func *log_writer = &pj_log_write; 33 33 static unsigned log_decor = PJ_LOG_HAS_TIME | PJ_LOG_HAS_MICRO_SEC | 34 PJ_LOG_HAS_SENDER | PJ_LOG_HAS_NEWLINE; 34 PJ_LOG_HAS_SENDER | PJ_LOG_HAS_NEWLINE | 35 PJ_LOG_HAS_SPACE | PJ_LOG_HAS_COLOR; 36 37 static pj_color_t PJ_LOG_COLOR_0 = PJ_TERM_COLOR_BRIGHT | PJ_TERM_COLOR_R; 38 static pj_color_t PJ_LOG_COLOR_1 = PJ_TERM_COLOR_BRIGHT | PJ_TERM_COLOR_R; 39 static pj_color_t PJ_LOG_COLOR_2 = PJ_TERM_COLOR_BRIGHT | 40 PJ_TERM_COLOR_R | 41 PJ_TERM_COLOR_G; 42 static pj_color_t PJ_LOG_COLOR_3 = PJ_TERM_COLOR_BRIGHT | 43 PJ_TERM_COLOR_R | 44 PJ_TERM_COLOR_G | 45 PJ_TERM_COLOR_B; 46 static pj_color_t PJ_LOG_COLOR_4 = PJ_TERM_COLOR_R | 47 PJ_TERM_COLOR_G | 48 PJ_TERM_COLOR_B; 49 static pj_color_t PJ_LOG_COLOR_5 = PJ_TERM_COLOR_R | 50 PJ_TERM_COLOR_G | 51 PJ_TERM_COLOR_B; 52 static pj_color_t PJ_LOG_COLOR_6 = PJ_TERM_COLOR_R | 53 PJ_TERM_COLOR_G | 54 PJ_TERM_COLOR_B; 55 /* Default terminal color */ 56 static pj_color_t PJ_LOG_COLOR_77 = PJ_TERM_COLOR_R | 57 PJ_TERM_COLOR_G | 58 PJ_TERM_COLOR_B; 35 59 36 60 #if PJ_LOG_USE_STACK_BUFFER==0 37 61 static char log_buffer[PJ_LOG_MAX_SIZE]; … … 47 71 return log_decor; 48 72 } 49 73 74 PJ_DEF(void) pj_log_set_color(int level, pj_color_t color) 75 { 76 switch (level) 77 { 78 case 0: PJ_LOG_COLOR_0 = color; 79 break; 80 case 1: PJ_LOG_COLOR_1 = color; 81 break; 82 case 2: PJ_LOG_COLOR_2 = color; 83 break; 84 case 3: PJ_LOG_COLOR_3 = color; 85 break; 86 case 4: PJ_LOG_COLOR_4 = color; 87 break; 88 case 5: PJ_LOG_COLOR_5 = color; 89 break; 90 case 6: PJ_LOG_COLOR_6 = color; 91 break; 92 /* Default terminal color */ 93 case 77: PJ_LOG_COLOR_77 = color; 94 break; 95 default: 96 /* Do nothing */ 97 break; 98 } 99 } 100 101 PJ_DEF(pj_color_t) pj_log_get_color(int level) 102 { 103 switch (level) { 104 case 0: 105 return PJ_LOG_COLOR_0; 106 case 1: 107 return PJ_LOG_COLOR_1; 108 case 2: 109 return PJ_LOG_COLOR_2; 110 case 3: 111 return PJ_LOG_COLOR_3; 112 case 4: 113 return PJ_LOG_COLOR_4; 114 case 5: 115 return PJ_LOG_COLOR_5; 116 case 6: 117 return PJ_LOG_COLOR_6; 118 default: 119 /* Return default terminal color */ 120 return PJ_LOG_COLOR_77; 121 } 122 } 123 50 124 PJ_DEF(void) pj_log_set_level(int level) 51 125 { 52 126 pj_log_max_level = level; … … 105 179 pre += pj_utoa_pad(ptime.mon+1, pre, 2, '0'); 106 180 } 107 181 if (log_decor & PJ_LOG_HAS_DAY_OF_MON) { 108 *pre++ = ' 182 *pre++ = '-'; 109 183 pre += pj_utoa_pad(ptime.day, pre, 2, '0'); 110 184 } 111 185 if (log_decor & PJ_LOG_HAS_TIME) { … … 139 213 if (log_decor != 0 && log_decor != PJ_LOG_HAS_NEWLINE) 140 214 *pre++ = ' '; 141 215 216 if (log_decor & PJ_LOG_HAS_SPACE) { 217 *pre++ = ' '; 218 *pre++ = ' '; 219 } 220 142 221 len = pre - log_buffer; 143 222 144 223 /* Print the whole message to the string log_buffer. */ -
pjsip-apps/src/pjsua/pjsua_app.c
3710 3710 3711 3711 len = write_settings(&app_config, settings, sizeof(settings)); 3712 3712 if (len < 1) 3713 PJ_LOG( 3,(THIS_FILE, "Error: not enough buffer"));3713 PJ_LOG(1,(THIS_FILE, "Error: not enough buffer")); 3714 3714 else 3715 3715 PJ_LOG(3,(THIS_FILE, 3716 3716 "Dumping configuration (%d bytes):\n%s\n", … … 3739 3739 3740 3740 len = write_settings(&app_config, settings, sizeof(settings)); 3741 3741 if (len < 1) 3742 PJ_LOG( 3,(THIS_FILE, "Error: not enough buffer"));3742 PJ_LOG(1,(THIS_FILE, "Error: not enough buffer")); 3743 3743 else { 3744 3744 pj_oshandle_t fd; 3745 3745 pj_status_t status; … … 4037 4037 #endif 4038 4038 4039 4039 if (transport_id == -1) { 4040 PJ_LOG( 3,(THIS_FILE, "Error: no transport is configured"));4040 PJ_LOG(1,(THIS_FILE, "Error: no transport is configured")); 4041 4041 status = -1; 4042 4042 goto on_error; 4043 4043 }