2012-07-29 03:05:53 +00:00
|
|
|
/*
|
2015-07-13 08:53:36 +00:00
|
|
|
Copyright (C) 2012-2015 Serval Project Inc.
|
2012-07-29 03:05:53 +00:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program 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 General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2013-12-04 06:44:14 +00:00
|
|
|
#ifndef __SERVAL_DNA__LOG_H
|
|
|
|
#define __SERVAL_DNA__LOG_H
|
2012-07-30 03:28:21 +00:00
|
|
|
|
2014-05-23 08:19:00 +00:00
|
|
|
#include <sys/types.h> // for size_t
|
|
|
|
#include <stdio.h> // for NULL
|
2012-11-29 06:14:06 +00:00
|
|
|
#include <stdlib.h>
|
2012-08-03 03:42:54 +00:00
|
|
|
#include <stdarg.h>
|
2012-11-29 06:42:43 +00:00
|
|
|
#include <errno.h>
|
2014-05-23 08:19:00 +00:00
|
|
|
#include "whence.h"
|
2013-03-18 05:04:33 +00:00
|
|
|
|
2014-05-05 08:47:51 +00:00
|
|
|
#ifndef __SERVAL_LOG_INLINE
|
|
|
|
# if __GNUC__ && !__GNUC_STDC_INLINE__
|
|
|
|
# define __SERVAL_LOG_INLINE extern inline
|
|
|
|
# else
|
|
|
|
# define __SERVAL_LOG_INLINE inline
|
|
|
|
# endif
|
|
|
|
#endif
|
2013-04-04 07:22:54 +00:00
|
|
|
|
2014-05-05 08:47:51 +00:00
|
|
|
// Logging levels.
|
|
|
|
#define LOG_LEVEL_INVALID (-1)
|
|
|
|
#define LOG_LEVEL_SILENT (0)
|
|
|
|
#define LOG_LEVEL_DEBUG (1)
|
|
|
|
#define LOG_LEVEL_INFO (2)
|
|
|
|
#define LOG_LEVEL_HINT (3)
|
|
|
|
#define LOG_LEVEL_WARN (4)
|
|
|
|
#define LOG_LEVEL_ERROR (5)
|
|
|
|
#define LOG_LEVEL_FATAL (6)
|
|
|
|
#define LOG_LEVEL_NONE (127)
|
|
|
|
const char *log_level_as_string(int level);
|
|
|
|
int string_to_log_level(const char *text);
|
|
|
|
|
|
|
|
// Log output control.
|
2014-06-11 05:23:26 +00:00
|
|
|
extern int logLevel_NoLogFileConfigured;
|
2013-03-26 07:54:37 +00:00
|
|
|
void close_log_file();
|
|
|
|
void disable_log_stderr();
|
2012-12-14 01:09:08 +00:00
|
|
|
void logFlush();
|
2013-03-28 13:52:08 +00:00
|
|
|
void logConfigChanged();
|
2013-09-01 08:03:32 +00:00
|
|
|
|
2015-06-22 08:37:50 +00:00
|
|
|
// Logging context string.
|
|
|
|
struct strbuf;
|
|
|
|
extern struct strbuf log_context;
|
|
|
|
|
2014-05-05 08:47:51 +00:00
|
|
|
// Logging primitives.
|
|
|
|
void vlogMessage(int level, struct __sourceloc whence, const char *fmt, va_list);
|
|
|
|
int logBacktrace(int level, struct __sourceloc whence);
|
2012-07-29 03:05:53 +00:00
|
|
|
|
2014-05-05 08:47:51 +00:00
|
|
|
__SERVAL_LOG_INLINE void logMessage(int level, struct __sourceloc whence, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vlogMessage(level, whence, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
2012-10-16 06:16:52 +00:00
|
|
|
|
2015-10-26 05:25:41 +00:00
|
|
|
__SERVAL_LOG_INLINE int logErrorAndReturnNegativeOne(struct __sourceloc whence, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vlogMessage(LOG_LEVEL_ERROR, whence, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-05-05 08:47:51 +00:00
|
|
|
// Useful logging primitive macros.
|
2012-10-16 06:16:52 +00:00
|
|
|
#define LOGF(L,F,...) logMessage(L, __WHENCE__, F, ##__VA_ARGS__)
|
2015-10-26 05:25:41 +00:00
|
|
|
#define LOGF_perror(L,F,...) logMessage(L, __WHENCE__, F ": %s [errno=%d]", ##__VA_ARGS__, strerror(errno), errno)
|
2012-07-29 03:05:53 +00:00
|
|
|
#define LOG_perror(L,X) LOGF_perror(L, "%s", (X))
|
|
|
|
|
2013-04-09 07:47:22 +00:00
|
|
|
#define NOWHENCE(LOGSTMT) do { const struct __sourceloc __whence = __NOWHENCE__; LOGSTMT; } while (0)
|
|
|
|
|
2015-07-06 08:19:49 +00:00
|
|
|
#define BACKTRACE logBacktrace(LOG_LEVEL_FATAL, __WHENCE__)
|
|
|
|
|
2012-11-12 04:07:35 +00:00
|
|
|
#define FATALF(F,...) do { LOGF(LOG_LEVEL_FATAL, F, ##__VA_ARGS__); abort(); exit(-1); } while (1)
|
2012-07-29 03:05:53 +00:00
|
|
|
#define FATAL(X) FATALF("%s", (X))
|
2012-08-21 04:04:08 +00:00
|
|
|
#define FATALF_perror(F,...) FATALF(F ": %s [errno=%d]", ##__VA_ARGS__, strerror(errno), errno)
|
|
|
|
#define FATAL_perror(X) FATALF_perror("%s", (X))
|
2012-07-29 03:05:53 +00:00
|
|
|
|
2015-10-26 05:25:41 +00:00
|
|
|
#define WHYF(F,...) logErrorAndReturnNegativeOne(__WHENCE__, F, ##__VA_ARGS__)
|
2012-07-29 03:05:53 +00:00
|
|
|
#define WHY(X) WHYF("%s", (X))
|
2015-10-26 05:25:41 +00:00
|
|
|
#define WHYF_perror(F,...) WHYF(F ": %s [errno=%d]", ##__VA_ARGS__, strerror(errno), errno)
|
2012-08-27 03:29:40 +00:00
|
|
|
#define WHY_perror(X) WHYF_perror("%s", (X))
|
2013-04-09 07:47:22 +00:00
|
|
|
#define WHY_argv(X,ARGC,ARGV) logArgv(LOG_LEVEL_ERROR, __WHENCE__, (X), (ARGC), (ARGV))
|
2012-07-29 03:05:53 +00:00
|
|
|
|
|
|
|
#define WARNF(F,...) LOGF(LOG_LEVEL_WARN, F, ##__VA_ARGS__)
|
|
|
|
#define WARN(X) WARNF("%s", (X))
|
2012-10-04 02:10:40 +00:00
|
|
|
#define WARNF_perror(F,...) LOGF_perror(LOG_LEVEL_WARN, F, ##__VA_ARGS__)
|
2012-08-27 03:29:40 +00:00
|
|
|
#define WARN_perror(X) WARNF_perror("%s", (X))
|
2013-04-09 07:47:22 +00:00
|
|
|
|
|
|
|
#define HINTF(F,...) LOGF(LOG_LEVEL_HINT, F, ##__VA_ARGS__)
|
|
|
|
#define HINT(X) HINTF("%s", (X))
|
|
|
|
#define HINT_argv(X,ARGC,ARGV) logArgv(LOG_LEVEL_HINT, __WHENCE__, (X), (ARGC), (ARGV))
|
2012-07-29 03:05:53 +00:00
|
|
|
|
|
|
|
#define INFOF(F,...) LOGF(LOG_LEVEL_INFO, F, ##__VA_ARGS__)
|
|
|
|
#define INFO(X) INFOF("%s", (X))
|
|
|
|
|
2015-07-13 08:53:36 +00:00
|
|
|
// log.h provides these macros for writing messages at DEBUG level, so applications can
|
|
|
|
// define their own DEBUG() and DEBUGF() macros; see "debug.h" as the prime example.
|
|
|
|
|
2015-07-06 08:19:49 +00:00
|
|
|
#define _DEBUGF(F,...) LOGF(LOG_LEVEL_DEBUG, F, ##__VA_ARGS__)
|
|
|
|
#define _DEBUG(X) _DEBUGF("%s", (X))
|
|
|
|
#define _DEBUGF_perror(F,...) LOGF_perror(LOG_LEVEL_DEBUG, F, ##__VA_ARGS__)
|
|
|
|
#define _DEBUG_perror(X) _DEBUGF_perror("%s", (X))
|
|
|
|
#define _DEBUG_argv(X,ARGC,ARGV) logArgv(LOG_LEVEL_DEBUG, __WHENCE__, (X), (ARGC), (ARGV))
|
|
|
|
|
|
|
|
#define _DEBUGF_TAG(TAG,F,...) _DEBUGF("{%s} " F, (TAG), ##__VA_ARGS__)
|
|
|
|
#define _DEBUGF_TAG_perror(TAG,F,...) _DEBUGF_perror("{%s} " F, (TAG), ##__VA_ARGS__)
|
|
|
|
#define _DEBUGF_TAG_argv(TAG,X,ARGC,ARGV) _DEBUGF_argv("{" TAG "} " X, (ARGC), (ARGV))
|
|
|
|
|
2012-10-16 06:16:52 +00:00
|
|
|
#define dump(X,A,N) logDump(LOG_LEVEL_DEBUG, __WHENCE__, (X), (const unsigned char *)(A), (size_t)(N))
|
2012-07-31 06:50:09 +00:00
|
|
|
|
2014-05-05 08:47:51 +00:00
|
|
|
// Utility functions, defined in terms of above primitives.
|
|
|
|
void logArgv(int level, struct __sourceloc whence, const char *label, int argc, const char *const *argv);
|
|
|
|
int logDump(int level, struct __sourceloc whence, char *name, const unsigned char *addr, size_t len);
|
|
|
|
void logString(int level, struct __sourceloc whence, const char *str);
|
2012-07-29 03:05:53 +00:00
|
|
|
|
2013-12-04 06:44:14 +00:00
|
|
|
#endif // __SERVAL_DNA__LOG_H
|