CPPMyth
Library to interoperate with MythTV server
Loading...
Searching...
No Matches
debug.cpp
1/*
2 * Copyright (C) 2014-2024 Jean-Luc Barriere
3 *
4 * This library is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published
6 * by the Free Software Foundation; either version 3, or (at your option)
7 * any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library; see the file COPYING. If not, write to
16 * the Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
17 * MA 02110-1301 USA
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 */
21
22#include "debug.h"
23
24#include <cstdlib>
25#include <cstdarg>
26#include <cstdio>
27#include <cstring>
28
29#if defined(_MSC_VER) && _MSC_VER < 1900
30#define snprintf _snprintf
31#endif
32
33
34int NSROOT::_dbgLevel = DBG_NONE;
35
36typedef struct
37{
38 const char* name;
39 void (*msg_callback)(int level, char* msg);
41
42static debug_ctx_t debug_ctx = {LIBTAG, nullptr};
43
44void NSROOT::_setDBGMsgCallback(void (*msgcb)(int level, char*))
45{
46 debug_ctx.msg_callback = msgcb;
47}
48
56static inline void __dbg(debug_ctx_t* ctx, int level, const char* fmt, va_list ap)
57{
58 if (ctx != nullptr)
59 {
60 char msg[4096];
61 int len = snprintf(msg, sizeof (msg), "(%s)", ctx->name);
62 vsnprintf(msg + len, sizeof (msg) - len, fmt, ap);
63 if (ctx->msg_callback)
64 {
65 ctx->msg_callback(level, msg);
66 }
67 else
68 {
69 fwrite(msg, strlen(msg), 1, stderr);
70 }
71 }
72}
73
74void NSROOT::_writeDBGMsg(int level, const char* fmt, ...)
75{
76 va_list ap;
77 va_start(ap, fmt);
78 __dbg(&debug_ctx, level, fmt, ap);
79 va_end(ap);
80}