summaryrefslogtreecommitdiffstats
path: root/src/shared/libosmocore/src/logging.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/shared/libosmocore/src/logging.c')
-rw-r--r--src/shared/libosmocore/src/logging.c67
1 files changed, 59 insertions, 8 deletions
diff --git a/src/shared/libosmocore/src/logging.c b/src/shared/libosmocore/src/logging.c
index 30316a5f..b3b5cb69 100644
--- a/src/shared/libosmocore/src/logging.c
+++ b/src/shared/libosmocore/src/logging.c
@@ -294,14 +294,11 @@ void log_set_category_filter(struct log_target *target, int category,
target->categories[category].loglevel = level;
}
-/* since C89/C99 says stderr is a macro, we can safely do this! */
-#ifdef stderr
-static void _stderr_output(struct log_target *target, const char *log)
+static void _file_output(struct log_target *target, const char *log)
{
- fprintf(target->tgt_stdout.out, "%s", log);
- fflush(target->tgt_stdout.out);
+ fprintf(target->tgt_file.out, "%s", log);
+ fflush(target->tgt_file.out);
}
-#endif
struct log_target *log_target_create(void)
{
@@ -340,14 +337,68 @@ struct log_target *log_target_create_stderr(void)
if (!target)
return NULL;
- target->tgt_stdout.out = stderr;
- target->output = _stderr_output;
+ target->tgt_file.out = stderr;
+ target->output = _file_output;
return target;
#else
return NULL;
#endif /* stderr */
}
+struct log_target *log_target_create_file(const char *fname)
+{
+ struct log_target *target;
+
+ target = log_target_create();
+ if (!target)
+ return NULL;
+
+ target->tgt_file.out = fopen(fname, "a");
+ if (!target->tgt_file.out)
+ return NULL;
+
+ target->output = _file_output;
+
+ target->tgt_file.fname = talloc_strdup(target, fname);
+
+ return target;
+}
+
+void log_target_destroy(struct log_target *target)
+{
+
+ /* just in case, to make sure we don't have any references */
+ log_del_target(target);
+
+ if (target->output == &_file_output) {
+/* since C89/C99 says stderr is a macro, we can safely do this! */
+#ifdef stderr
+ /* don't close stderr */
+ if (target->tgt_file.out != stderr)
+#endif
+ {
+ fclose(target->tgt_file.out);
+ target->tgt_file.out = NULL;
+ }
+ }
+
+ talloc_free(target);
+}
+
+/* close and re-open a log file (for log file rotation) */
+int log_target_file_reopen(struct log_target *target)
+{
+ fclose(target->tgt_file.out);
+
+ target->tgt_file.out = fopen(target->tgt_file.fname, "a");
+ if (!target->tgt_file.out)
+ return -errno;
+
+ /* we assume target->output already to be set */
+
+ return 0;
+}
+
const char *log_vty_level_string(struct log_info *info)
{
const struct value_string *vs;