From ac5c021c93d4394b3bad6bf02a4dab6ee6a4b169 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Sun, 3 Apr 2011 10:41:27 +0200 Subject: [PATCH] Move DEBUG() from Utils.h into new ZNCDebug.h Signed-off-by: Uli Schlachter --- Makefile.in | 2 +- Modules.h | 2 +- Utils.cpp | 19 ++++++------------- Utils.h | 26 -------------------------- ZNCDebug.cpp | 17 +++++++++++++++++ ZNCDebug.h | 46 ++++++++++++++++++++++++++++++++++++++++++++++ defines.h | 3 ++- main.cpp | 12 ++++++------ znc.cpp | 4 ++-- 9 files changed, 81 insertions(+), 50 deletions(-) create mode 100644 ZNCDebug.cpp create mode 100644 ZNCDebug.h diff --git a/Makefile.in b/Makefile.in index 6111cd64..b244f9d5 100644 --- a/Makefile.in +++ b/Makefile.in @@ -31,7 +31,7 @@ INSTALL_DATA := @INSTALL_DATA@ LIB_SRCS := ZNCString.cpp Csocket.cpp znc.cpp User.cpp IRCSock.cpp Client.cpp DCCBounce.cpp \ DCCSock.cpp Chan.cpp Nick.cpp Server.cpp Modules.cpp MD5.cpp Buffer.cpp Utils.cpp \ FileUtils.cpp HTTPSock.cpp Template.cpp ClientCommand.cpp Socket.cpp SHA256.cpp \ - WebModules.cpp Listener.cpp Config.cpp + WebModules.cpp Listener.cpp Config.cpp ZNCDebug.cpp BIN_SRCS := main.cpp LIB_OBJS := $(patsubst %cpp,%o,$(LIB_SRCS)) BIN_OBJS := $(patsubst %cpp,%o,$(BIN_SRCS)) diff --git a/Modules.h b/Modules.h index 10076e17..a2a1215c 100644 --- a/Modules.h +++ b/Modules.h @@ -11,7 +11,7 @@ #include "zncconfig.h" #include "WebModules.h" -#include "Utils.h" +#include "main.h" #include #include diff --git a/Utils.cpp b/Utils.cpp index 627ce7de..cf7069f9 100644 --- a/Utils.cpp +++ b/Utils.cpp @@ -9,6 +9,7 @@ #include "Utils.h" #include "MD5.h" #include "main.h" +#include "ZNCDebug.h" #include #ifdef HAVE_LIBSSL #include @@ -24,14 +25,6 @@ using std::stringstream; -bool CUtils::stdoutIsTTY = true; -bool CUtils::debug = -#ifdef _DEBUG - true; -#else - false; -#endif - CUtils::CUtils() {} CUtils::~CUtils() {} @@ -271,7 +264,7 @@ bool CUtils::GetInput(const CString& sPrompt, CString& sRet, const CString& sDef } void CUtils::PrintError(const CString& sMessage) { - if (stdoutIsTTY) + if (CDebug::StdoutIsTTY()) fprintf(stdout, "\033[1m\033[34m[\033[31m ** \033[34m]\033[39m\033[22m %s\n", sMessage.c_str()); else fprintf(stdout, "%s\n", sMessage.c_str()); @@ -279,7 +272,7 @@ void CUtils::PrintError(const CString& sMessage) { } void CUtils::PrintPrompt(const CString& sMessage) { - if (stdoutIsTTY) + if (CDebug::StdoutIsTTY()) fprintf(stdout, "\033[1m\033[34m[\033[33m ?? \033[34m]\033[39m\033[22m %s: ", sMessage.c_str()); else fprintf(stdout, "[ ?? ] %s: ", sMessage.c_str()); @@ -287,7 +280,7 @@ void CUtils::PrintPrompt(const CString& sMessage) { } void CUtils::PrintMessage(const CString& sMessage, bool bStrong) { - if (stdoutIsTTY) { + if (CDebug::StdoutIsTTY()) { if (bStrong) fprintf(stdout, "\033[1m\033[34m[\033[33m ** \033[34m]\033[39m\033[22m \033[1m%s\033[22m\n", sMessage.c_str()); @@ -301,7 +294,7 @@ void CUtils::PrintMessage(const CString& sMessage, bool bStrong) { } void CUtils::PrintAction(const CString& sMessage) { - if (stdoutIsTTY) + if (CDebug::StdoutIsTTY()) fprintf(stdout, "\033[1m\033[34m[\033[32m \033[34m]\033[39m\033[22m %s... ", sMessage.c_str()); else fprintf(stdout, "%s... ", sMessage.c_str()); @@ -309,7 +302,7 @@ void CUtils::PrintAction(const CString& sMessage) { } void CUtils::PrintStatus(bool bSuccess, const CString& sMessage) { - if (stdoutIsTTY) { + if (CDebug::StdoutIsTTY()) { if (!sMessage.empty()) { if (bSuccess) { fprintf(stdout, "%s", sMessage.c_str()); diff --git a/Utils.h b/Utils.h index 1b67a0a5..5dc337a5 100644 --- a/Utils.h +++ b/Utils.h @@ -19,30 +19,10 @@ #include #include #include -#include using std::map; using std::vector; using std::pair; -using std::cout; -using std::endl; - -/** Output a debug info if debugging is enabled. - * If ZNC was compiled with --enable-debug or was started with - * --debug, the given argument will be sent to stdout. - * - * You can use all the features of C++ streams: - * @code - * DEBUG("I had " << errors << " errors"); - * @endcode - * - * @param f The expression you want to display. - */ -#define DEBUG(f) do { \ - if (CUtils::Debug()) { \ - cout << f << endl; \ - } \ -} while (0) static inline void SetFdCloseOnExec(int fd) { @@ -62,10 +42,6 @@ public: static CString GetIP(unsigned long addr); static unsigned long GetLongIP(const CString& sIP); - static void SetStdoutIsTTY(bool b) { stdoutIsTTY = b; } - static bool StdoutIsTTY() { return stdoutIsTTY; } - static void SetDebug(bool b) { debug = b; } - static bool Debug() { return debug; } static void PrintError(const CString& sMessage); static void PrintMessage(const CString& sMessage, bool bStrong = false); @@ -99,8 +75,6 @@ public: private: protected: - static bool stdoutIsTTY; - static bool debug; }; class CException { diff --git a/ZNCDebug.cpp b/ZNCDebug.cpp new file mode 100644 index 00000000..4586aec2 --- /dev/null +++ b/ZNCDebug.cpp @@ -0,0 +1,17 @@ +/* + * Copyright (C) 2004-2011 See the AUTHORS file for details. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 as published + * by the Free Software Foundation. + */ + +#include "ZNCDebug.h" + +bool CDebug::stdoutIsTTY = true; +bool CDebug::debug = +#ifdef _DEBUG + true; +#else + false; +#endif diff --git a/ZNCDebug.h b/ZNCDebug.h new file mode 100644 index 00000000..83c17fec --- /dev/null +++ b/ZNCDebug.h @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2004-2011 See the AUTHORS file for details. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 as published + * by the Free Software Foundation. + */ + +#ifndef ZNCDEBUG_H +#define ZNCDEBUG_H + +#include + +using std::cout; +using std::endl; + +/** Output a debug info if debugging is enabled. + * If ZNC was compiled with --enable-debug or was started with + * --debug, the given argument will be sent to stdout. + * + * You can use all the features of C++ streams: + * @code + * DEBUG("I had " << errors << " errors"); + * @endcode + * + * @param f The expression you want to display. + */ +#define DEBUG(f) do { \ + if (CDebug::Debug()) { \ + cout << f << endl; \ + } \ +} while (0) + +class CDebug { +public: + static void SetStdoutIsTTY(bool b) { stdoutIsTTY = b; } + static bool StdoutIsTTY() { return stdoutIsTTY; } + static void SetDebug(bool b) { debug = b; } + static bool Debug() { return debug; } + +protected: + static bool stdoutIsTTY; + static bool debug; +}; + +#endif // !ZNCDEBUG_H diff --git a/defines.h b/defines.h index d76b58a7..4ffee4f4 100644 --- a/defines.h +++ b/defines.h @@ -13,7 +13,8 @@ // This header file is just for Csocket -#include "Utils.h" +#include "ZNCDebug.h" +#include "ZNCString.h" #define CS_STRING CString #define _NO_CSOCKET_NS diff --git a/main.cpp b/main.cpp index 50703a19..71717a90 100644 --- a/main.cpp +++ b/main.cpp @@ -117,7 +117,7 @@ int main(int argc, char** argv) { CString sDataDir = ""; seedPRNG(); - CUtils::SetStdoutIsTTY(isatty(1)); + CDebug::SetStdoutIsTTY(isatty(1)); int iArg, iOptIndex = -1; bool bMakeConf = false; @@ -142,7 +142,7 @@ int main(int argc, char** argv) { cout << CZNC::GetTag() << endl; return 0; case 'n': - CUtils::SetStdoutIsTTY(false); + CDebug::SetStdoutIsTTY(false); break; case 'r': bAllowRoot = true; @@ -166,7 +166,7 @@ int main(int argc, char** argv) { break; case 'D': bForeground = true; - CUtils::SetDebug(true); + CDebug::SetDebug(true); break; case '?': default: @@ -285,7 +285,7 @@ int main(int argc, char** argv) { close(1); open("/dev/null", O_WRONLY); close(2); open("/dev/null", O_WRONLY); - CUtils::SetStdoutIsTTY(false); + CDebug::SetStdoutIsTTY(false); // We are the child. There is no way we can be a process group // leader, thus setsid() must succeed. @@ -334,11 +334,11 @@ int main(int argc, char** argv) { NULL }; int pos = 3; - if (CUtils::Debug()) + if (CDebug::Debug()) args[pos++] = strdup("--debug"); else if (bForeground) args[pos++] = strdup("--foreground"); - if (!CUtils::StdoutIsTTY()) + if (!CDebug::StdoutIsTTY()) args[pos++] = strdup("--no-color"); if (bAllowRoot) args[pos++] = strdup("--allow-root"); diff --git a/znc.cpp b/znc.cpp index 2e5245de..424a84d6 100644 --- a/znc.cpp +++ b/znc.cpp @@ -677,7 +677,7 @@ bool CZNC::WriteNewConfig(const CString& sConfigFile) { const CModInfo& Info = *it; CString sName = Info.GetName(); - if (CUtils::StdoutIsTTY()) { + if (CDebug::StdoutIsTTY()) { if (CUtils::GetBoolInput("Load global module <\033[1m" + sName + "\033[22m>?", false)) vsLines.push_back("LoadModule = " + sName); } else { @@ -783,7 +783,7 @@ bool CZNC::WriteNewConfig(const CString& sConfigFile) { const CModInfo& Info = *it; CString sName = Info.GetName(); - if (CUtils::StdoutIsTTY()) { + if (CDebug::StdoutIsTTY()) { if (CUtils::GetBoolInput("Load module <\033[1m" + sName + "\033[22m>?", false)) vsLines.push_back("\tLoadModule = " + sName); } else {