Use the app_info->name instead of the hostname
[osmocom-bb.git] / src / logging.c
index 30316a5..b3b5cb6 100644 (file)
@@ -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;