Changeset 793cce15 in mainline
- Timestamp:
- 2012-08-16T14:22:56Z (13 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 32b26cf7
- Parents:
- 9a53e00
- Location:
- uspace
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/io/log.c
r9a53e00 r793cce15 86 86 } 87 87 88 static int logger_message(async_sess_t *session, log_ level_t level, const char *message)88 static int logger_message(async_sess_t *session, log_context_t ctx, log_level_t level, const char *message) 89 89 { 90 90 async_exch_t *exchange = async_exchange_begin(session); … … 93 93 } 94 94 95 aid_t reg_msg = async_send_1(exchange, LOGGER_MESSAGE, level, NULL); 95 aid_t reg_msg = async_send_2(exchange, LOGGER_MESSAGE, 96 ctx, level, NULL); 96 97 int rc = async_data_write_start(exchange, message, str_size(message)); 97 98 sysarg_t reg_msg_rc; … … 241 242 } 242 243 243 bool _log_shall_record(log_level_t level) 244 /** Create logging context. 245 * 246 * This function always returns a valid context. 247 */ 248 log_context_t log_context_create(const char *name) 249 { 250 async_exch_t *exchange = async_exchange_begin(logger_session); 251 if (exchange == NULL) 252 return LOG_CONTEXT_DEFAULT; 253 254 ipc_call_t answer; 255 aid_t reg_msg = async_send_0(exchange, LOGGER_CREATE_CONTEXT, &answer); 256 int rc = async_data_write_start(exchange, name, str_size(name)); 257 sysarg_t reg_msg_rc; 258 async_wait_for(reg_msg, ®_msg_rc); 259 260 async_exchange_end(exchange); 261 262 if ((rc != EOK) || (reg_msg_rc != EOK)) 263 return LOG_CONTEXT_DEFAULT; 264 265 return IPC_GET_ARG1(answer); 266 } 267 268 bool _log_shall_record(log_context_t context, log_level_t level) 244 269 { 245 270 return get_current_observed_level() >= level; … … 253 278 * @param fmt Format string (no traling newline). 254 279 */ 255 void _log_ msg(log_level_t level, const char *fmt, ...)280 void _log_ctx_msg(log_context_t ctx, log_level_t level, const char *fmt, ...) 256 281 { 257 282 va_list args; 258 283 259 284 va_start(args, fmt); 260 _log_ msgv(level, fmt, args);285 _log_ctx_msgv(ctx, level, fmt, args); 261 286 va_end(args); 262 287 } … … 269 294 * @param fmt Format string (no trailing newline) 270 295 */ 271 void _log_ msgv(log_level_t level, const char *fmt, va_list args)296 void _log_ctx_msgv(log_context_t ctx, log_level_t level, const char *fmt, va_list args) 272 297 { 273 298 assert(level < LVL_LIMIT); … … 283 308 284 309 vsnprintf(message_buffer, MESSAGE_BUFFER_SIZE, fmt, args); 285 logger_message(logger_session, level, message_buffer);310 logger_message(logger_session, ctx, level, message_buffer); 286 311 } 287 312 -
uspace/lib/c/include/io/log.h
r9a53e00 r793cce15 36 36 37 37 #include <stdarg.h> 38 #include <inttypes.h> 38 39 #include <bool.h> 39 40 … … 50 51 } log_level_t; 51 52 53 typedef sysarg_t log_context_t; 54 #define PRIlogctx PRIxn 55 #define LOG_CONTEXT_DEFAULT 0 56 52 57 extern const char *log_level_str(log_level_t); 53 58 extern int log_level_from_str(const char *, log_level_t *); 54 59 55 extern bool _log_shall_record(log_ level_t);60 extern bool _log_shall_record(log_context_t, log_level_t); 56 61 extern int log_init(const char *, log_level_t); 57 62 58 #define log_msg(level, format, ...) \ 63 extern log_context_t log_context_create(const char *); 64 65 #define log_ctx_msg(context, level, format, ...) \ 59 66 do { \ 60 if (_log_shall_record(( level))) { \61 _log_ msg(level, format, ##__VA_ARGS__); \67 if (_log_shall_record((context), (level))) { \ 68 _log_ctx_msg((context), (level), format, ##__VA_ARGS__); \ 62 69 } \ 63 70 } while (false) 64 71 65 #define log_ msgv(level, format, args) \72 #define log_ctx_msgv(context, level, format, args) \ 66 73 do { \ 67 if (_log_shall_record(( level))) { \68 _log_ msgv(level, format, args); \74 if (_log_shall_record((context), (level))) { \ 75 _log_ctx_msgv((context), (level), format, args); \ 69 76 } \ 70 77 } while (false) 71 78 72 extern void _log_msg(log_level_t, const char *, ...); 73 extern void _log_msgv(log_level_t, const char *, va_list); 79 #define log_msg(level, format, ...) \ 80 log_ctx_msg(LOG_CONTEXT_DEFAULT, (level), (format), ##__VA_ARGS__) 81 #define log_msgv(level, format, args) \ 82 log_ctx_msgv(LOG_CONTEXT_DEFAULT, (level), (format), (args)) 83 84 extern void _log_ctx_msg(log_context_t, log_level_t, const char *, ...); 85 extern void _log_ctx_msgv(log_context_t, log_level_t, const char *, va_list); 74 86 75 87 #endif -
uspace/lib/c/include/ipc/logger.h
r9a53e00 r793cce15 44 44 typedef enum { 45 45 LOGGER_REGISTER = IPC_FIRST_USER_METHOD, 46 LOGGER_CREATE_CONTEXT, 46 47 LOGGER_MESSAGE, 47 48 LOGGER_BLOCK_UNTIL_READER_CHANGED -
uspace/srv/logger/logger.h
r9a53e00 r793cce15 57 57 58 58 void namespace_wait_for_reader_change(logging_namespace_t *, bool *); 59 bool namespace_has_reader(logging_namespace_t *, log_level_t); 60 void namespace_add_message(logging_namespace_t *, const char *, log_level_t); 59 bool namespace_has_reader(logging_namespace_t *, sysarg_t, log_level_t); 60 void namespace_add_message(logging_namespace_t *, const char *, sysarg_t, log_level_t); 61 62 int namespace_create_context(logging_namespace_t *, const char *); 61 63 62 64 log_level_t get_default_logging_level(void); -
uspace/srv/logger/main.c
r9a53e00 r793cce15 126 126 } 127 127 128 static int handle_receive_message(logging_namespace_t *namespace, int level)129 { 130 bool skip_message = !namespace_has_reader(namespace, level);128 static int handle_receive_message(logging_namespace_t *namespace, sysarg_t context, int level) 129 { 130 bool skip_message = !namespace_has_reader(namespace, context, level); 131 131 if (skip_message) { 132 132 /* Abort the actual message buffer transfer. */ … … 147 147 } 148 148 149 namespace_add_message(namespace, message, level);149 namespace_add_message(namespace, message, context, level); 150 150 151 151 free(message); 152 152 153 return EOK; 154 } 155 156 static int handle_create_context(logging_namespace_t *namespace, sysarg_t *idx) 157 { 158 void *name; 159 int rc = async_data_write_accept(&name, true, 0, 0, 0, NULL); 160 if (rc != EOK) { 161 return rc; 162 } 163 164 rc = namespace_create_context(namespace, name); 165 166 free(name); 167 168 if (rc < 0) 169 return rc; 170 171 *idx = (sysarg_t) rc; 153 172 return EOK; 154 173 } … … 166 185 167 186 int rc; 187 sysarg_t arg = 0; 168 188 169 189 switch (IPC_GET_IMETHOD(call)) { 190 case LOGGER_CREATE_CONTEXT: 191 rc = handle_create_context(namespace, &arg); 192 async_answer_1(callid, rc, arg); 193 break; 170 194 case LOGGER_MESSAGE: 171 rc = handle_receive_message(namespace, IPC_GET_ARG1(call) );195 rc = handle_receive_message(namespace, IPC_GET_ARG1(call), IPC_GET_ARG2(call)); 172 196 async_answer_0(callid, rc); 173 197 break; -
uspace/srv/logger/namespace.c
r9a53e00 r793cce15 41 41 */ 42 42 43 #define CONTEXT_SIZE 16 44 45 typedef struct { 46 const char *name; 47 log_level_t level; 48 } logging_context_t; 43 49 44 50 struct logging_namespace { … … 50 56 const char *name; 51 57 58 // FIXME: make dynamic 59 size_t context_count; 60 logging_context_t context[CONTEXT_SIZE]; 61 52 62 link_t link; 53 63 }; … … 56 66 static LIST_INITIALIZE(namespace_list); 57 67 58 static log_level_t namespace_get_actual_log_level(logging_namespace_t *namespace) 59 { 60 fibril_mutex_lock(&namespace->guard); 61 log_level_t level = namespace->level; 68 static log_level_t namespace_get_actual_log_level(logging_namespace_t *namespace, sysarg_t context) 69 { 70 fibril_mutex_lock(&namespace->guard); 71 if (context >= namespace->context_count) { 72 fibril_mutex_unlock(&namespace->guard); 73 return LVL_FATAL; 74 } 75 log_level_t level = namespace->context[context].level; 62 76 fibril_mutex_unlock(&namespace->guard); 63 77 … … 115 129 namespace->level = LOG_LEVEL_USE_DEFAULT; 116 130 131 namespace->context_count = 1; 132 namespace->context[0].name = ""; 133 namespace->context[0].level = LOG_LEVEL_USE_DEFAULT; 134 117 135 fibril_mutex_initialize(&namespace->guard); 118 136 fibril_condvar_initialize(&namespace->level_changed_cv); … … 206 224 fibril_mutex_lock(&namespace->guard); 207 225 namespace->level = level; 226 for (size_t i = 0; i < namespace->context_count; i++) { 227 namespace->context[i].level = level; 228 } 208 229 fibril_condvar_broadcast(&namespace->level_changed_cv); 209 230 fibril_mutex_unlock(&namespace->guard); … … 213 234 214 235 215 bool namespace_has_reader(logging_namespace_t *namespace, log_level_t level) 216 { 217 return level <= namespace_get_actual_log_level(namespace); 236 bool namespace_has_reader(logging_namespace_t *namespace, sysarg_t context, log_level_t level) 237 { 238 return level <= namespace_get_actual_log_level(namespace, context); 239 } 240 241 int namespace_create_context(logging_namespace_t *namespace, const char *name) 242 { 243 int rc; 244 fibril_mutex_lock(&namespace->guard); 245 if (namespace->context_count >= CONTEXT_SIZE) { 246 rc = ELIMIT; 247 goto leave; 248 } 249 250 namespace->context[namespace->context_count].level 251 = LOG_LEVEL_USE_DEFAULT; 252 namespace->context[namespace->context_count].name 253 = str_dup(name); 254 if (namespace->context[namespace->context_count].name == NULL) { 255 rc = ENOMEM; 256 goto leave; 257 } 258 rc = (int) namespace->context_count; 259 namespace->context_count++; 260 261 262 leave: 263 fibril_mutex_unlock(&namespace->guard); 264 return rc; 218 265 } 219 266 … … 230 277 231 278 232 void namespace_add_message(logging_namespace_t *namespace, const char *message, log_level_t level)233 { 234 if (level <= namespace_get_actual_log_level(namespace )) {279 void namespace_add_message(logging_namespace_t *namespace, const char *message, sysarg_t context, log_level_t level) 280 { 281 if (level <= namespace_get_actual_log_level(namespace, context)) { 235 282 const char *level_name = log_level_str(level); 236 printf("[%s %s]: %s\n", namespace->name, level_name, message); 237 fprintf(namespace->logfile, "%s: %s\n", level_name, message); 283 if (context == 0) { 284 printf("[%s %s]: %s\n", 285 namespace->name, level_name, message); 286 fprintf(namespace->logfile, "%s: %s\n", 287 level_name, message); 288 } else { 289 const char *context_name = namespace->context[context].name; 290 printf("[%s/%s %s]: %s\n", 291 namespace->name, context_name, level_name, message); 292 fprintf(namespace->logfile, "[%s] %s: %s\n", 293 context_name, level_name, message); 294 } 238 295 fflush(namespace->logfile); 239 296 fflush(stdout);
Note:
See TracChangeset
for help on using the changeset viewer.