Changeset 1f2dd20 in mainline for uspace/lib/c/generic/io/log.c
- Timestamp:
- 2012-07-04T12:57:00Z (13 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- f47c70d4
- Parents:
- 5e4f22b
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/io/log.c
r5e4f22b r1f2dd20 38 38 #include <stdlib.h> 39 39 #include <stdio.h> 40 #include <async.h> 41 #include <io/log.h> 42 #include <ipc/logger.h> 43 #include <ns.h> 40 44 41 #include <io/log.h> 42 43 /** Serialization mutex for logging functions. */ 44 static FIBRIL_MUTEX_INITIALIZE(log_serializer); 45 46 /** Current log level. */ 47 static log_level_t log_level; 48 49 static FILE *log_stream; 50 45 /** Log messages are printed under this name. */ 51 46 static const char *log_prog_name; 52 47 53 /** Prefixes for individual logging levels. */ 54 static const char *log_level_names[] = { 55 [LVL_FATAL] = "Fatal error", 56 [LVL_ERROR] = "Error", 57 [LVL_WARN] = "Warning", 58 [LVL_NOTE] = "Note", 59 [LVL_DEBUG] = "Debug", 60 [LVL_DEBUG2] = "Debug2" 61 }; 48 /** IPC session with the logger service. */ 49 static async_sess_t *logger_session; 50 51 /** Maximum length of a single log message (in bytes). */ 52 #define MESSAGE_BUFFER_SIZE 4096 53 54 static int logger_register(async_sess_t *session, const char *prog_name) 55 { 56 async_exch_t *exchange = async_exchange_begin(session); 57 if (exchange == NULL) { 58 return ENOMEM; 59 } 60 61 aid_t reg_msg = async_send_0(exchange, LOGGER_REGISTER, NULL); 62 int rc = async_data_write_start(exchange, prog_name, str_size(prog_name)); 63 sysarg_t reg_msg_rc; 64 async_wait_for(reg_msg, ®_msg_rc); 65 66 async_exchange_end(exchange); 67 68 if (rc != EOK) { 69 return rc; 70 } 71 72 return reg_msg_rc; 73 } 74 75 static int logger_message(async_sess_t *session, log_level_t level, const char *message) 76 { 77 async_exch_t *exchange = async_exchange_begin(session); 78 if (exchange == NULL) { 79 return ENOMEM; 80 } 81 82 aid_t reg_msg = async_send_1(exchange, LOGGER_MESSAGE, level, NULL); 83 int rc = async_data_write_start(exchange, message, str_size(message)); 84 sysarg_t reg_msg_rc; 85 async_wait_for(reg_msg, ®_msg_rc); 86 87 async_exchange_end(exchange); 88 89 if (rc != EOK) { 90 return rc; 91 } 92 93 return reg_msg_rc; 94 } 62 95 63 96 /** Initialize the logging system. … … 69 102 { 70 103 assert(level < LVL_LIMIT); 71 log_level = level;72 104 73 log_stream = stdout;74 105 log_prog_name = str_dup(prog_name); 75 106 if (log_prog_name == NULL) 76 107 return ENOMEM; 77 108 78 return EOK; 109 logger_session = service_connect_blocking(EXCHANGE_SERIALIZE, SERVICE_LOGGER, LOGGER_INTERFACE_SINK, 0); 110 if (logger_session == NULL) { 111 return ENOMEM; 112 } 113 114 int rc = logger_register(logger_session, log_prog_name); 115 116 return rc; 79 117 } 80 118 … … 106 144 assert(level < LVL_LIMIT); 107 145 108 /* Higher number means higher verbosity. */ 109 if (level <= log_level) { 110 fibril_mutex_lock(&log_serializer); 146 char *message_buffer = malloc(MESSAGE_BUFFER_SIZE); 147 if (message_buffer == NULL) { 148 return; 149 } 111 150 112 fprintf(log_stream, "%s: %s: ", log_prog_name, 113 log_level_names[level]); 114 vfprintf(log_stream, fmt, args); 115 fputc('\n', log_stream); 116 fflush(log_stream); 117 118 fibril_mutex_unlock(&log_serializer); 119 } 151 vsnprintf(message_buffer, MESSAGE_BUFFER_SIZE, fmt, args); 152 logger_message(logger_session, level, message_buffer); 120 153 } 121 154
Note:
See TracChangeset
for help on using the changeset viewer.