libvisual  0.5.0
lv_log.c
1 /* Libvisual - The audio visualisation framework.
2  *
3  * Copyright (C) 2012 Libvisual team
4  * 2004-2006 Dennis Smit
5  *
6  * Authors: Chong Kai Xiong <kaixiong@codeleft.sg>
7  * Dennis Smit <ds@nerds-incorporated.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser General Public License as
11  * published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */
23 
24 #include <config.h>
25 #include "lv_log.h"
26 #include "lv_common.h"
27 #include "lv_util.h"
28 #include <stdio.h>
29 #include <stdarg.h>
30 #include <string.h>
31 
32 #define LV_LOG_MAX_MESSAGE_SIZE 1024
33 
34 typedef struct {
35  VisLogHandlerFunc func;
36  void *priv;
37 } LogHandler;
38 
39 static VisLogSeverity verbosity = VISUAL_LOG_WARNING;
40 
41 static const char *log_prefixes[VISUAL_LOG_NUM_LEVELS] = {
42  "DEBUG ",
43  "INFO ",
44  "WARNING ",
45  "ERROR ",
46  "CRITICAL "
47 };
48 
49 static LogHandler log_handlers[VISUAL_LOG_NUM_LEVELS];
50 
51 static void output_to_stderr (VisLogSeverity severity, const char *msg,
52  VisLogSource const *source);
53 
54 static int is_valid_severity (VisLogSeverity severity)
55 {
56  return (severity >= VISUAL_LOG_DEBUG && severity < VISUAL_LOG_NUM_LEVELS);
57 }
58 
60 {
61  verbosity = level;
62 }
63 
65 {
66  return verbosity;
67 }
68 
69 void visual_log_set_handler (VisLogSeverity severity, VisLogHandlerFunc func, void *priv)
70 {
71  LogHandler *handler;
72 
73  visual_return_if_fail (is_valid_severity (severity));
74 
75  handler = &log_handlers[severity];
76  handler->func = func;
77  handler->priv = priv;
78 }
79 
80 void _lv_log (VisLogSeverity severity,
81  const char *file, int line, const char *funcname,
82  const char *fmt, ...)
83 {
84  VisLogSource source;
85 
86  LogHandler *handler;
87  char message[LV_LOG_MAX_MESSAGE_SIZE];
88  va_list va;
89 
90  if (!is_valid_severity (severity) || fmt == NULL) {
91  visual_log (VISUAL_LOG_ERROR, "(malformed message)");
92  return;
93  }
94 
95  if (verbosity > severity) {
96  return;
97  }
98 
99  va_start (va, fmt);
100  vsnprintf (message, LV_LOG_MAX_MESSAGE_SIZE-1, fmt, va);
101  va_end (va);
102 
103  source.file = visual_truncate_path (file, 3);
104  source.func = funcname;
105  source.line = line;
106 
107  handler = &log_handlers[severity];
108 
109  if (handler->func != NULL) {
110  handler->func (severity, message, &source, handler->priv);
111  } else {
112  output_to_stderr (severity, message, &source);
113  }
114 }
115 
116 static void output_to_stderr (VisLogSeverity severity, const char *msg,
117  VisLogSource const *source)
118 {
119  fprintf (stderr, "%s %s:%d:%s: %s\n", log_prefixes[severity],
120  source->file, source->line, source->func, msg);
121  fflush (stderr);
122 }