]> Shamusworld >> Repos - architektonas/blob - src/base/debug.cpp
Removed unnecessary RS_ prefix from classes and whatnot.
[architektonas] / src / base / debug.cpp
1 // debug.cpp
2 //
3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // Portions copyright (C) 2001-2003 RibbonSoft
7 // Copyright (C) 2010 Underground Software
8 // See the README and GPLv2 files for licensing and warranty information
9 //
10 // JLH = James L. Hammons <jlhamm@acm.org>
11 //
12 // Who  When        What
13 // ---  ----------  -----------------------------------------------------------
14 // JLH  05/28/2010  Added this text. :-)
15 //
16
17 #include "debug.h"
18
19 #include <stdarg.h>
20 #include "system.h"
21
22 Debug * Debug::uniqueInstance = NULL;
23
24 /**
25  * Gets the one and only Debug instance (creates a new one on first call only)
26  *
27  * @return Pointer to the single instance of this singleton class
28  */
29 Debug * Debug::instance()
30 {
31         if (uniqueInstance == NULL)
32         {
33                 QDateTime now = QDateTime::currentDateTime();
34                 QString nowStr;
35                 nowStr = now.toString("yyyyMMdd_hhmmss");
36                 QString fName = QString("debug_%1.log").arg(nowStr);
37
38                 uniqueInstance = new Debug;
39 //              uniqueInstance->stream = fopen(fName.toAscii().data(), "wt");
40 //              uniqueInstance->stream = stderr;
41                 uniqueInstance->stream = stdout;
42         }
43
44         return uniqueInstance;
45 }
46
47 /**
48  * Deletes the one and only Debug instance.
49  */
50 void Debug::deleteInstance()
51 {
52         if (uniqueInstance)
53         {
54                 fclose(uniqueInstance->stream);
55                 delete uniqueInstance;
56         }
57 }
58
59 /**
60  * Constructor for a point with default coordinates.
61  */
62 Debug::Debug()
63 {
64         debugLevel = D_DEBUGGING;
65 }
66
67 /**
68  * Sets the debugging level.
69  */
70 void Debug::setLevel(DebugLevel level)
71 {
72         debugLevel = level;
73         print("DEBUG: Warnings", D_WARNING);
74         print("DEBUG: Errors", D_ERROR);
75         print("DEBUG: Notice", D_NOTICE);
76         print("DEBUG: Informational", D_INFORMATIONAL);
77         print("DEBUG: Debugging", D_DEBUGGING);
78 }
79
80 /**
81  * Gets the current debugging level.
82  */
83 Debug::DebugLevel Debug::getLevel()
84 {
85         return debugLevel;
86 }
87
88 /**
89  * Prints the given message to stdout.
90  */
91 void Debug::print(const char * format ...)
92 {
93         if (debugLevel == D_DEBUGGING)
94         {
95                 va_list ap;
96                 va_start(ap, format);
97                 vfprintf(stream, format, ap);
98                 fprintf(stream, "\n");
99                 va_end(ap);
100                 fflush(stream);
101         }
102 }
103
104 /**
105  * Prints the given message to stdout if the current debug level
106  * is lower then the given level
107  *
108  * @param level Debug level.
109  */
110 void Debug::print(DebugLevel level, const char * format ...)
111 {
112         if (debugLevel >= level)
113         {
114                 va_list ap;
115                 va_start(ap, format);
116                 vfprintf(stream, format, ap);
117                 fprintf(stream, "\n");
118                 va_end(ap);
119                 fflush(stream);
120         }
121 }
122
123 /**
124  * Prints a time stamp in the format yyyyMMdd_hhmmss.
125  */
126 void Debug::timestamp()
127 {
128         QDateTime now = QDateTime::currentDateTime();
129         QString nowStr = now.toString("yyyyMMdd_hh:mm:ss:zzz ");
130         fprintf(stream, "%s", nowStr.toAscii().data());
131         fprintf(stream, "\n");
132         fflush(stream);
133 }
134
135 /**
136  * Prints the unicode for every character in the given string.
137  */
138 void Debug::printUnicode(const QString & text)
139 {
140         for(int i=0; i<(int)text.length(); i++)
141                 print("[%X] %c", text.at(i).unicode(), text.at(i).toAscii());
142 }
143
144 void Debug::setStream(FILE * s)
145 {
146         stream = s;
147 }