1 /*******************************************************************************
2 **
3 ** Photivo
4 **
5 ** Copyright (C) 2012 Michael Munzert <mail@mm-log.com>
6 ** Copyright (C) 2012 Bernd Schoeler <brjohn@brother-john.net>
7 **
8 ** This file is part of Photivo.
9 **
10 ** Photivo is free software: you can redistribute it and/or modify
11 ** it under the terms of the GNU General Public License version 3
12 ** as published by the Free Software Foundation.
13 **
14 ** Photivo is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ** GNU General Public License for more details.
18 **
19 ** You should have received a copy of the GNU General Public License
20 ** along with Photivo.  If not, see <http://www.gnu.org/licenses/>.
21 **
22 *******************************************************************************/
23 
24 #include "ptInfo.h"
25 
26 #include <QApplication>
27 #include <QMessageBox>
28 
29 #include <iostream>
30 
31 //==============================================================================
32 
33 ptInfo* ptInfo::FInstance = nullptr;
34 
35 //==============================================================================
36 
37 /*! Global variable to the info system.*/
38 ptInfo* GInfo;
39 
40 //==============================================================================
41 
42 /*! \class GetGlobalInfo
43   Helper to instantiate and terminate the info system.
44   */
45 class GetGlobalInfo
46 {
47 public:
GetGlobalInfo()48   GetGlobalInfo() {GInfo = ptInfo::GetInstance();}
~GetGlobalInfo()49   ~GetGlobalInfo() {ptInfo::ShutDownInfoSystem();}
50 };
51 
52 GetGlobalInfo GlobalInfo;
53 
54 //==============================================================================
55 // This method needs to do everything needed for shutting the info system down.
ShutDownInfoSystem()56 void ptInfo::ShutDownInfoSystem()
57 {
58   if (FInstance) delete FInstance;
59   FInstance = 0;
60 }
61 
62 //==============================================================================
63 
Log(const char * AMsg,const short AMode)64 void ptInfo::Log(const char *AMsg, const short AMode)
65 {
66   QString hTemp = QString(AMsg);
67   if (!hTemp.trimmed().isEmpty()) {
68     if (AMode == 0 || !qApp->activeWindow()) {
69       std::cout << hTemp.toLocal8Bit().data() << std::endl;
70     } else if (AMode == 1) {
71       QMessageBox::information(nullptr, "Photivo log", hTemp);
72     }
73   }
74 }
75 
76 //==============================================================================
77 
Log(const QString & AMsg,const short AMode)78 void ptInfo::Log(const QString &AMsg, const short AMode)
79 {
80   Log(AMsg.toLocal8Bit().data(), AMode);
81 }
82 
83 //==============================================================================
84 
Raise(const char * AMsg,const char * ALocation)85 void ptInfo::Raise(const char *AMsg, const char *ALocation)
86 {
87   QString hTemp = QString(AMsg);
88   if (QString(ALocation).trimmed() != "") {
89     hTemp += CRLF;
90     hTemp += QString(ALocation);
91   }
92 
93   Log(hTemp);
94   throw;
95 }
96 
97 //==============================================================================
98 
Raise(const QString AMsg,const char * ALocation)99 void ptInfo::Raise(const QString AMsg, const char *ALocation)
100 {
101   Raise(AMsg.toLocal8Bit().data(), ALocation);
102 }
103 
104 //==============================================================================
105 
Warning(const char * AMsg,const char * ALocation)106 void ptInfo::Warning(const char *AMsg, const char *ALocation) {
107   QString hTemp = QString(AMsg);
108   if (QString(ALocation).trimmed() != "") {
109     hTemp += CRLF;
110     hTemp += QString(ALocation);
111   }
112 
113   Log(hTemp);
114 }
115 
116 //==============================================================================
117 
Warning(const QString AMsg,const char * ALocation)118 void ptInfo::Warning(const QString AMsg, const char *ALocation) {
119   Warning(AMsg.toLocal8Bit().data(), ALocation);
120 }
121 
122 //==============================================================================
123 
StartTimer(const char * AMsg)124 void ptInfo::StartTimer(const char *AMsg)
125 {
126   FTimerComplete->start();
127   FTimerDiff->start();
128   Log(AMsg);
129 }
130 
131 //==============================================================================
132 
GetInstance()133 ptInfo *ptInfo::GetInstance()
134 {
135   if (!FInstance) {
136     FInstance = new ptInfo();
137   }
138 
139   return FInstance;
140 }
141 
142 //==============================================================================
143 
ptInfo()144 ptInfo::ptInfo()
145 {
146   FTimerComplete = new QTime();
147   FTimerDiff     = new QTime();
148 
149   // printf("**********\nInfo system started.\n**********\n");
150 }
151 
152 //==============================================================================
153 
~ptInfo()154 ptInfo::~ptInfo()
155 {
156   delete FTimerComplete;
157   delete FTimerDiff;
158 
159   // printf("**********\nInfo system shut down.\n**********\n");
160 }
161 
162 //==============================================================================
163 
StopTimer(const char * AMsg)164 void ptInfo::StopTimer(const char *AMsg)
165 {
166   // Just for symmetry.
167   // The timer is not actually running, nothing to stop.
168   QString hTemp = QString(AMsg).trimmed();
169   if (!hTemp.isEmpty()) {
170     hTemp += ": ";
171   }
172   hTemp += QString::number(FTimerComplete->elapsed());
173 
174   Log(hTemp, 1);
175 }
176 
177 //==============================================================================
178 
ShowMsg(const char * AMsg,const char * ALocation)179 void ptInfo::ShowMsg(const char *AMsg, const char *ALocation)
180 {
181   QString hTemp = QString(AMsg);
182   if (QString(ALocation).trimmed() != "") {
183     hTemp += CRLF;
184     hTemp += QString(ALocation);
185   }
186 
187   Log(hTemp, 1);
188 }
189 
190 //==============================================================================
191 
ShowMsg(const QString AMsg,const char * ALocation)192 void ptInfo::ShowMsg(const QString AMsg, const char *ALocation)
193 {
194   ShowMsg(AMsg.toLocal8Bit().data(), ALocation);
195 }
196 
197 //==============================================================================
198 
LogTimerInterval(const char * AMsg)199 void ptInfo::LogTimerInterval(const char *AMsg)
200 {
201   QString hTemp = QString(AMsg).trimmed();
202   if (!hTemp.isEmpty()) {
203     hTemp += ": ";
204   }
205   hTemp += QString::number(FTimerDiff->restart());
206 
207   Log(hTemp);
208 }
209 
210 //==============================================================================
211 
Assert(const bool ACondition,const char * AMsg,const char * ALocation)212 void ptInfo::Assert(const bool ACondition, const char *AMsg, const char *ALocation) {
213   if (!ACondition)
214     Raise(AMsg, ALocation);
215 }
216 
217 //==============================================================================
218 
Assert(const bool ACondition,const QString AMsg,const char * ALocation)219 void ptInfo::Assert(const bool ACondition, const QString AMsg, const char *ALocation) {
220   if (!ACondition)
221     Raise(AMsg.toLocal8Bit().data(), ALocation);
222 }
223 
224 //==============================================================================
225