]> Shamusworld >> Repos - architektonas/blob - src/base/rs_debug.cpp
Adding missing implementation.
[architektonas] / src / base / rs_debug.cpp
1 // rs_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 "rs_debug.h"
18
19 #include <stdarg.h>
20 #include "rs_system.h"
21
22 RS_Debug * RS_Debug::uniqueInstance = NULL;
23
24 /**
25  *  Gets the one and only RS_Debug instance
26  *  (creates a new one on first call only)
27  *
28  *  @return Pointer to the single instance of this
29  * singleton class
30  */
31 RS_Debug * RS_Debug::instance()
32 {
33         if (uniqueInstance == NULL)
34         {
35                 QDateTime now = QDateTime::currentDateTime();
36                 QString nowStr;
37                 nowStr = now.toString("yyyyMMdd_hhmmss");
38                 QString fName = QString("debug_%1.log").arg(nowStr);
39
40                 uniqueInstance = new RS_Debug;
41                 //uniqueInstance->stream = fopen(fName.latin1(), "wt");
42                 uniqueInstance->stream = stderr;
43         }
44
45         return uniqueInstance;
46 }
47
48 /**
49  * Deletes the one and only RS_Debug instance.
50  */
51 void RS_Debug::deleteInstance()
52 {
53         if (uniqueInstance != NULL)
54         {
55                 fclose(uniqueInstance->stream);
56                 delete uniqueInstance;
57         }
58 }
59
60 /**
61  * Constructor for a point with default coordinates.
62  */
63 RS_Debug::RS_Debug()
64 {
65         debugLevel = D_DEBUGGING;
66 }
67
68 /**
69  * Sets the debugging level.
70  */
71 void RS_Debug::setLevel(RS_DebugLevel level)
72 {
73         debugLevel = level;
74         print("RS_DEBUG: Warnings", D_WARNING);
75         print("RS_DEBUG: Errors", D_ERROR);
76         print("RS_DEBUG: Notice", D_NOTICE);
77         print("RS_DEBUG: Informational", D_INFORMATIONAL);
78         print("RS_DEBUG: Debugging", D_DEBUGGING);
79 }
80
81 /**
82  * Gets the current debugging level.
83  */
84 RS_Debug::RS_DebugLevel RS_Debug::getLevel()
85 {
86         return debugLevel;
87 }
88
89 /**
90  * Prints the given message to stdout.
91  */
92 void RS_Debug::print(const char * format ...)
93 {
94         if (debugLevel == D_DEBUGGING)
95         {
96                 va_list ap;
97                 va_start(ap, format);
98                 vfprintf(stream, format, ap);
99                 fprintf(stream, "\n");
100                 va_end(ap);
101                 fflush(stream);
102         }
103 }
104
105 /**
106  * Prints the given message to stdout if the current debug level
107  * is lower then the given level
108  *
109  * @param level Debug level.
110  */
111 void RS_Debug::print(RS_DebugLevel level, const char * format ...)
112 {
113         if (debugLevel >= level)
114         {
115                 va_list ap;
116                 va_start(ap, format);
117                 vfprintf(stream, format, ap);
118                 fprintf(stream, "\n");
119                 va_end(ap);
120                 fflush(stream);
121         }
122 }
123
124 /**
125  * Prints a time stamp in the format yyyyMMdd_hhmmss.
126  */
127 void RS_Debug::timestamp()
128 {
129         QDateTime now = QDateTime::currentDateTime();
130         QString nowStr = now.toString("yyyyMMdd_hh:mm:ss:zzz ");
131         fprintf(stream, "%s", nowStr.toLatin1().data());
132         fprintf(stream, "\n");
133         fflush(stream);
134 }
135
136 /**
137  * Prints the unicode for every character in the given string.
138  */
139 void RS_Debug::printUnicode(const QString & text)
140 {
141         for(int i=0; i<(int)text.length(); i++)
142                 print("[%X] %c", text.at(i).unicode(), text.at(i).toLatin1());
143 }
144
145 void RS_Debug::setStream(FILE * s)
146 {
147         stream = s;
148 }