Changeset 90dc458 in mainline
- Timestamp:
- 2012-08-17T11:38:08Z (12 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- ae2c925
- Parents:
- 5239e17
- Location:
- uspace/srv/logger
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/logger/initlvl.c
r5239e17 r90dc458 35 35 */ 36 36 #include <errno.h> 37 #include <str_error.h> 37 38 #include <sysinfo.h> 38 39 #include <str.h> … … 61 62 return; 62 63 63 logger_log_t *log = find_or_create_log_and_acquire(key, 0); 64 if (log == NULL) 64 logger_log_t *log; 65 rc = find_or_create_log_and_acquire(key, 0, &log); 66 if (rc != EOK) 65 67 return; 66 68 -
uspace/srv/logger/logger.h
r5239e17 r90dc458 51 51 52 52 typedef struct { 53 fibril_mutex_t guard; 54 char *filename; 53 55 FILE *logfile; 54 56 } logger_dest_t; … … 67 69 68 70 logger_log_t *find_log_by_name_and_acquire(const char *name); 69 logger_log_t *find_or_create_log_and_acquire(const char *name, sysarg_t parent);71 int find_or_create_log_and_acquire(const char *, sysarg_t, logger_log_t **); 70 72 logger_log_t *find_log_by_id_and_acquire(sysarg_t); 71 73 bool shall_log_message(logger_log_t *, log_level_t); 72 74 void log_release(logger_log_t *); 75 void write_to_log(logger_log_t *, log_level_t, const char *); 73 76 74 77 log_level_t get_default_logging_level(void); -
uspace/srv/logger/logs.c
r5239e17 r90dc458 42 42 43 43 44 static logger_log_t *find_log_by_name_and_parent_no_l ock(const char *name, logger_log_t *parent)44 static logger_log_t *find_log_by_name_and_parent_no_list_lock_and_acquire(const char *name, logger_log_t *parent) 45 45 { 46 46 list_foreach(log_list, it) { 47 47 logger_log_t *log = list_get_instance(it, logger_log_t, link); 48 if ((parent == log->parent) && (str_cmp(log->name, name) == 0)) 48 if ((parent == log->parent) && (str_cmp(log->name, name) == 0)) { 49 fibril_mutex_lock(&log->guard); 49 50 return log; 51 } 50 52 } 51 53 … … 58 60 if (result == NULL) 59 61 return ENOMEM; 60 char *logfilename; 61 int rc = asprintf(&logfilename, "/log/%s", name); 62 int rc = asprintf(&result->filename, "/log/%s", name); 62 63 if (rc < 0) { 63 64 free(result); 64 65 return ENOMEM; 65 66 } 66 result->logfile = fopen(logfilename, "a"); 67 free(logfilename); 68 if (result->logfile == NULL) { 69 free(result); 70 return ENOMEM; 71 } 67 result->logfile = NULL; 68 fibril_mutex_initialize(&result->guard); 72 69 *dest = result; 73 70 return EOK; 74 71 } 75 72 76 logger_log_t *find_or_create_log_and_acquire(const char *name, sysarg_t parent_id) 77 { 73 int find_or_create_log_and_acquire(const char *name, sysarg_t parent_id, logger_log_t **log_out) 74 { 75 int rc; 78 76 logger_log_t *result = NULL; 79 77 logger_log_t *parent = (logger_log_t *) parent_id; … … 81 79 fibril_mutex_lock(&log_list_guard); 82 80 83 result = find_log_by_name_and_parent_no_lock(name, parent); 84 if (result != NULL) 81 result = find_log_by_name_and_parent_no_list_lock_and_acquire(name, parent); 82 if (result != NULL) { 83 rc = EOK; 85 84 goto leave; 85 } 86 86 87 87 result = calloc(1, sizeof(logger_log_t)); 88 if (result == NULL) 88 if (result == NULL) { 89 rc = ENOMEM; 89 90 goto leave; 91 } 90 92 91 93 result->logged_level = LOG_LEVEL_USE_DEFAULT; … … 93 95 if (parent == NULL) { 94 96 result->full_name = str_dup(name); 95 intrc = create_dest(name, &result->dest);97 rc = create_dest(name, &result->dest); 96 98 if (rc != EOK) 97 99 goto error_result_allocated; 98 100 } else { 99 intrc = asprintf(&result->full_name, "%s/%s",101 rc = asprintf(&result->full_name, "%s/%s", 100 102 parent->full_name, name); 101 103 if (rc < 0) … … 116 118 fibril_mutex_unlock(&log_list_guard); 117 119 118 return result; 120 if (rc == EOK) { 121 assert(fibril_mutex_is_locked(&result->guard)); 122 *log_out = result; 123 } 124 125 return rc; 119 126 120 127 error_result_allocated: … … 125 132 fibril_mutex_unlock(&log_list_guard); 126 133 127 return NULL;134 return rc; 128 135 } 129 136 … … 190 197 } 191 198 199 void write_to_log(logger_log_t *log, log_level_t level, const char *message) 200 { 201 assert(fibril_mutex_is_locked(&log->guard)); 202 assert(log->dest != NULL); 203 fibril_mutex_lock(&log->dest->guard); 204 if (log->dest->logfile == NULL) 205 log->dest->logfile = fopen(log->dest->filename, "a"); 206 207 if (log->dest->logfile != NULL) { 208 fprintf(log->dest->logfile, "[%s] %s: %s\n", 209 log->full_name, log_level_str(level), 210 (const char *) message); 211 fflush(log->dest->logfile); 212 } 213 214 fibril_mutex_unlock(&log->dest->guard); 215 } 216 192 217 /** 193 218 * @} -
uspace/srv/logger/writer.c
r5239e17 r90dc458 56 56 return NULL; 57 57 58 logger_log_t *log = find_or_create_log_and_acquire(name, parent); 58 logger_log_t *log = NULL; 59 rc = find_or_create_log_and_acquire(name, parent, &log); 60 if (rc) 59 61 60 62 free(name); … … 82 84 log->full_name, log_level_str(level), 83 85 (const char *) message); 84 fprintf(log->dest->logfile, "[%s] %s: %s\n", 85 log->full_name, log_level_str(level), 86 (const char *) message); 87 fflush(log->dest->logfile); 86 write_to_log(log, level, message); 88 87 89 88 rc = EOK;
Note:
See TracChangeset
for help on using the changeset viewer.