ucx/logging.c

changeset 39
3e55bed345f9
parent 17
11dffb40cd91
child 70
88092b88ec00
equal deleted inserted replaced
38:b855f76e965b 39:3e55bed345f9
59 void ucx_logger_free(UcxLogger *logger) { 59 void ucx_logger_free(UcxLogger *logger) {
60 ucx_map_free(logger->levels); 60 ucx_map_free(logger->levels);
61 free(logger); 61 free(logger);
62 } 62 }
63 63
64 // estimated max. message length (documented)
65 #define UCX_LOGGER_MSGMAX 4096
66
64 void ucx_logger_logf(UcxLogger *logger, unsigned int level, const char* file, 67 void ucx_logger_logf(UcxLogger *logger, unsigned int level, const char* file,
65 const unsigned int line, const char *format, ...) { 68 const unsigned int line, const char *format, ...) {
66 if (level <= logger->level) { 69 if (level <= logger->level) {
67 const size_t max = 4096; // estimated max. message length (documented) 70 char msg[UCX_LOGGER_MSGMAX];
68 char msg[max];
69 char *text; 71 char *text;
70 size_t k = 0; 72 size_t k = 0;
71 size_t n; 73 size_t n;
72 74
73 if ((logger->mask & UCX_LOGGER_LEVEL) > 0) { 75 if ((logger->mask & UCX_LOGGER_LEVEL) > 0) {
83 } 85 }
84 if ((logger->mask & UCX_LOGGER_SOURCE) > 0) { 86 if ((logger->mask & UCX_LOGGER_SOURCE) > 0) {
85 n = strlen(file); 87 n = strlen(file);
86 memcpy(msg+k, file, n); 88 memcpy(msg+k, file, n);
87 k += n; 89 k += n;
88 k += sprintf(msg+k, ":%d ", line); 90 #ifdef _WIN32
91 k += _snprintf(msg+k, UCX_LOGGER_MSGMAX-k, ":%d ", line);
92 #else
93 k += snprintf(msg+k, UCX_LOGGER_MSGMAX-k, ":%d ", line);
94 #endif /* _WIN32 */
89 } 95 }
90 96
91 msg[k++] = '-'; msg[k++] = ' '; 97 msg[k++] = '-'; msg[k++] = ' ';
92 98
93 va_list args; 99 va_list args;
94 va_start (args, format); 100 va_start (args, format);
95 k += vsnprintf(msg+k, max-k-1, format, args); 101 k += vsnprintf(msg+k, UCX_LOGGER_MSGMAX-k-1, format, args);
96 va_end (args); 102 va_end (args);
97 103
98 msg[k++] = '\n'; 104 msg[k++] = '\n';
99 105
100 logger->writer(msg, 1, k, logger->stream); 106 logger->writer(msg, 1, k, logger->stream);

mercurial