Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Revamp logging stuff

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
  • Loading branch information...
commit ebd73d0aa17fd8a697c8238848da957a6c6472d9 1 parent 4c57824
@felipec authored
Showing with 93 additions and 57 deletions.
  1. +4 −0 dummy_arm.c
  2. +65 −31 log.c
  3. +24 −26 log.h
View
4 dummy_arm.c
@@ -172,8 +172,10 @@ handle_options(int *argc,
if (cmd[0] != '-')
break;
+#ifdef DEBUG
if (!strcmp(cmd, "-d") || !strcmp(cmd, "--debug"))
debug_level = 3;
+#endif
if (!strcmp(cmd, "-n") || !strcmp(cmd, "--ntimes")) {
if (*argc < 2) {
@@ -199,7 +201,9 @@ main(int argc,
signal(SIGINT, signal_handler);
+#ifdef DEBUG
debug_level = 2;
+#endif
ntimes = 1000;
argc--; argv++;
View
96 log.c
@@ -1,29 +1,43 @@
/*
- * Copyright (C) 2009 Nokia Corporation.
+ * Copyright (C) 2009-2010 Felipe Contreras
+ * Copyright (C) 2009-2010 Nokia Corporation
*
- * Author: Felipe Contreras <felipe.contreras@nokia.com>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation
- * version 2.1 of the License.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * Author: Felipe Contreras <felipe.contreras@gmail.com>
*
+ * This file may be used under the terms of the GNU Lesser General Public
+ * License version 2.1, a copy of which is found in LICENSE included in the
+ * packaging of this file.
*/
+#include "log.h"
+
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
-unsigned int debug_level = 1;
+#define SYSLOG
+
+#ifdef SYSLOG
+#include <syslog.h>
+#endif
+
+#ifdef DEBUG
+unsigned debug_level = 2;
+#endif
+
+#ifdef SYSLOG
+static inline int
+log_level_to_syslog(unsigned int level)
+{
+ switch (level) {
+ case 0: return LOG_ERR;
+ case 1: return LOG_WARNING;
+ case 2:
+ case 3: return LOG_INFO;
+ default: return LOG_DEBUG;
+ }
+}
+#endif
static inline const char *
log_level_to_string(unsigned int level)
@@ -31,36 +45,56 @@ log_level_to_string(unsigned int level)
switch (level) {
case 0: return "error"; break;
case 1: return "warning"; break;
- case 2: return "info"; break;
- case 3: return "debug"; break;
+ case 2: return "test"; break;
+ case 3: return "info"; break;
+ case 4: return "debug"; break;
default: return NULL; break;
}
}
-void
-pr_helper(unsigned int level,
- const char *file,
- const char *function,
- unsigned int line,
- const char *fmt,
- ...)
+void pr_helper(unsigned int level,
+ const char *file,
+ const char *function,
+ unsigned int line,
+ const char *fmt,
+ ...)
{
char *tmp;
va_list args;
+#ifdef DEBUG
if (level > debug_level)
return;
+#endif
va_start(args, fmt);
- vasprintf(&tmp, fmt, args);
+ if (vasprintf(&tmp, fmt, args) < 0)
+ goto leave;
- fprintf(stderr, "%s %s:%d:%s() %s\n",
- log_level_to_string(level),
- file, line, function,
- tmp);
+ if (level <= 1) {
+#ifdef SYSLOG
+ syslog(log_level_to_syslog(level), "%s", tmp);
+#endif
+ fprintf(stderr, "%s: %s: %s\n",
+ log_level_to_string(level), function, tmp);
+ }
+ else if (level == 2)
+ fprintf(stderr, "%s: %s:%s(%u): %s\n",
+ log_level_to_string(level), file, function, line, tmp);
+#if defined(DEVEL) || defined(DEBUG)
+ else if (level == 3)
+ fprintf(stderr, "%s: %s: %s\n",
+ log_level_to_string(level), function, tmp);
+#endif
+#ifdef DEBUG
+ else if (level == 4)
+ fprintf(stderr, "%s: %s:%s(%u): %s\n",
+ log_level_to_string(level), file, function, line, tmp);
+#endif
free(tmp);
+leave:
va_end(args);
}
View
50 log.h
@@ -1,42 +1,40 @@
/*
- * Copyright (C) 2009 Nokia Corporation.
+ * Copyright (C) 2009-2010 Felipe Contreras
+ * Copyright (C) 2009-2010 Nokia Corporation
*
- * Author: Felipe Contreras <felipe.contreras@nokia.com>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation
- * version 2.1 of the License.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * Author: Felipe Contreras <felipe.contreras@gmail.com>
*
+ * This file may be used under the terms of the GNU Lesser General Public
+ * License version 2.1, a copy of which is found in LICENSE included in the
+ * packaging of this file.
*/
#ifndef LOG_H
#define LOG_H
-extern int debug_level;
+#ifdef DEBUG
+extern unsigned debug_level;
+#endif
-void
-pr_helper(unsigned int level,
- const char *file,
- const char *function,
- unsigned int line,
- const char *fmt,
- ...);
+void pr_helper(unsigned int level,
+ const char *file,
+ const char *function,
+ unsigned int line,
+ const char *fmt,
+ ...) __attribute__((format(printf, 5, 6)));
#define pr_base(level, ...) pr_helper(level, __FILE__, __func__, __LINE__, __VA_ARGS__)
#define pr_err(...) pr_base(0, __VA_ARGS__)
#define pr_warning(...) pr_base(1, __VA_ARGS__)
-#define pr_info(...) pr_base(2, __VA_ARGS__)
-#define pr_debug(...) pr_base(3, __VA_ARGS__)
+#define pr_test(...) pr_base(2, __VA_ARGS__)
+
+#ifdef DEBUG
+#define pr_info(...) pr_base(3, __VA_ARGS__)
+#define pr_debug(...) pr_base(4, __VA_ARGS__)
+#else
+#define pr_info(...) ({ if (0) pr_base(3, __VA_ARGS__); })
+#define pr_debug(...) ({ if (0) pr_base(4, __VA_ARGS__); })
+#endif
#endif /* LOG_H */
Please sign in to comment.
Something went wrong with that request. Please try again.