Skip to content

Commit b3a404f

Browse files
committed
Distinguish log type
1 parent fa447f8 commit b3a404f

9 files changed

+81
-63
lines changed

src/Core.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ void Core::Tick() {
5656

5757
if( wasGameRunning != mGameRunning ) {
5858
if( mGameRunning ) {
59-
LOG("Hearthstone found");
59+
INFO("Hearthstone found");
6060
} else {
61-
LOG("Hearthstone was closed");
61+
INFO("Hearthstone was closed");
6262
Hearthstone::Instance()->SetRestartRequired( false );
6363
}
6464
}
@@ -96,7 +96,7 @@ void Core::HandleMatchStart() {
9696

9797
void Core::HandleMatchEnd( const ::CardHistoryList& cardHistoryList, bool wasSpectating ) {
9898
if( wasSpectating ) {
99-
LOG( "Ignore spectated match" );
99+
INFO( "Ignore spectated match" );
100100
ResetResult();
101101
return;
102102
}

src/Hearthstone.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ void Hearthstone::SetWindowCapture( WindowCapture *windowCapture ) {
6464
}
6565

6666
void Hearthstone::EnableLogging() {
67-
const int NUM_LOG_MODULES = 4;
68-
const char LOG_MODULES[ NUM_LOG_MODULES ][ 32 ] = { "Zone", "Asset", "Bob", "Power" };
67+
const int NUM_INFO_MODULES = 4;
68+
const char INFO_MODULES[ NUM_INFO_MODULES ][ 32 ] = { "Zone", "Asset", "Bob", "Power" };
6969

7070
string path = LogConfigPath();
7171
QFile file( path.c_str() );
@@ -82,17 +82,17 @@ void Hearthstone::EnableLogging() {
8282
}
8383

8484
if( !file.open( QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text ) ) {
85-
LOG( "Couldn't create file %s", path.c_str() );
85+
ERROR( "Couldn't create file %s", path.c_str() );
8686
} else {
8787
QTextStream out( &file );
88-
for( int i = 0; i < NUM_LOG_MODULES; i++ ) {
89-
const char *logModuleName = LOG_MODULES[ i ];
88+
for( int i = 0; i < NUM_INFO_MODULES; i++ ) {
89+
const char *logModuleName = INFO_MODULES[ i ];
9090
if( !contents.contains( QString( "[%1]" ).arg( logModuleName ) ) ) {
9191
out << "\n";
92-
out << "[" << LOG_MODULES[i] << "]\n";
92+
out << "[" << INFO_MODULES[i] << "]\n";
9393
out << "LogLevel=1\n";
9494
out << "ConsolePrinting=true\n";
95-
LOG( "Enable Log Module %s", logModuleName );
95+
INFO( "Enable Log Module %s", logModuleName );
9696

9797
if( Running() ) {
9898
SetRestartRequired( true );
@@ -107,7 +107,7 @@ void Hearthstone::DisableLogging() {
107107
QFile file( LogConfigPath().c_str() );
108108
if( file.exists() ) {
109109
file.remove();
110-
LOG( "Ingame log deactivated." );
110+
INFO( "Ingame log deactivated." );
111111
}
112112
}
113113

@@ -132,7 +132,7 @@ string Hearthstone::LogPath() {
132132
QSettings hsKey( "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Hearthstone", QSettings::NativeFormat );
133133
QString hsPath = hsKey.value( "InstallLocation" ).toString();
134134
if( hsPath.isEmpty() ) {
135-
LOG( "LogPath Fallback" );
135+
INFO( "LogPath Fallback" );
136136
QString programFiles( getenv( "PROGRAMFILES(X86)" ) );
137137
if( programFiles.isEmpty() ) {
138138
programFiles = getenv( "PROGRAMFILES" );

src/Local.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,12 @@ typedef vector< CardHistoryItem > CardHistoryList;
137137

138138

139139
#include "Logger.h"
140-
#define LOG(str, ...) Logger::Instance()->Add(str, ##__VA_ARGS__)
140+
141+
#define INFO(str, ...) Logger::Instance()->Add(LOG_INFO, str, ##__VA_ARGS__)
142+
#define ERROR(str, ...) Logger::Instance()->Add(LOG_ERROR, str, ##__VA_ARGS__)
141143

142144
#ifdef _DEBUG
143-
#define DEBUG(str, ...) Logger::Instance()->Add("[DEBUG] "str, ##__VA_ARGS__)
145+
#define DEBUG(str, ...) Logger::Instance()->Add(LOG_DEBUG, str, ##__VA_ARGS__)
144146
#else
145147
#define DEBUG(str, ...)
146148
#endif

src/Logger.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,31 @@ void Logger::SetLogPath( const string& path ) {
1414
mOf.open( path.c_str(), std::ios_base::app );
1515
}
1616

17-
void Logger::Add( const char *fmt, ... ) {
17+
void Logger::Add( LogEventType type, const char *fmt, ... ) {
1818
char buffer[ 4096 ];
1919

20+
// Parse vargs
2021
va_list args;
2122
va_start( args, fmt );
2223
vsnprintf( buffer, sizeof(buffer), fmt, args );
2324
va_end( args );
2425

25-
Add( string( buffer ) );
26-
}
27-
28-
void Logger::Add( const string& message ) {
2926
// Timestamp
3027
char timestamp[ 256 ];
3128
time_t t = time( 0 );
3229
struct tm *now = localtime( &t );
33-
strftime( timestamp, sizeof( timestamp ), "[%H:%M:%S] ", now );
30+
strftime( timestamp, sizeof( timestamp ), "%H:%M:%S", now );
3431

35-
string line = string( timestamp ) + message + "\n";
32+
// Decorate
33+
char decorated[ 4096 ];
34+
sprintf( decorated, "[%s] %s: %s\n", timestamp, LOG_EVENT_TYPE_NAMES[ type ], buffer );
35+
string line = string( decorated );
3636

3737
// Add to file
3838
if( mOf.is_open() ) {
3939
mOf << line;
4040
mOf.flush();
4141
}
4242

43-
emit NewMessage( line );
43+
emit NewMessage( type, line );
4444
}

src/Logger.h

+15-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@
99

1010
#include <QDebug>
1111

12+
typedef enum {
13+
LOG_DEBUG = 0,
14+
LOG_INFO = 1,
15+
LOG_ERROR = 2
16+
} LogEventType;
17+
18+
const char LOG_EVENT_TYPE_NAMES[][128] = {
19+
"DEBUG",
20+
"INFO",
21+
"ERROR"
22+
};
23+
1224
class Logger : public QObject {
1325
Q_OBJECT;
1426

@@ -17,12 +29,12 @@ DEFINE_SINGLETON( Logger )
1729
private:
1830
std::ofstream mOf;
1931

32+
2033
public:
2134
void SetLogPath( const string& path );
22-
void Add( const char *fmt, ... );
23-
void Add( const string& message );
35+
void Add( LogEventType type, const char *fmt, ... );
2436

2537
signals:
26-
void NewMessage( const string& message );
38+
void NewMessage( LogEventType type, const string& message );
2739
};
2840

src/Main.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ int main( int argc, char **argv )
6969
Logger::Instance()->SetLogPath( logFilePath );
7070

7171
// Start
72-
LOG( "--> Launched v%s on %s", VERSION, QDate::currentDate().toString( Qt::ISODate ).toStdString().c_str() );
72+
INFO( "--> Launched v%s on %s", VERSION, QDate::currentDate().toString( Qt::ISODate ).toStdString().c_str() );
7373

7474
#if defined Q_OS_MAC
7575
CocoaInitializer cocoaInitializer;
@@ -91,7 +91,7 @@ int main( int argc, char **argv )
9191
int exitCode = app.exec();
9292

9393
// Tear down
94-
LOG( "<-- Shutdown" );
94+
INFO( "<-- Shutdown" );
9595

9696
return exitCode;
9797
}

src/Tracker.cpp

+18-27
Original file line numberDiff line numberDiff line change
@@ -30,68 +30,59 @@ Tracker::~Tracker() {
3030

3131
void Tracker::EnsureAccountIsSetUp() {
3232
if( !IsAccountSetUp() ) {
33-
LOG( "No account setup. Creating one for you." );
33+
INFO( "No account setup. Creating one for you." );
3434
CreateAndStoreAccount();
3535
} else {
36-
LOG( "Account %s found", Username().toStdString().c_str() );
36+
INFO( "Account %s found", Username().toStdString().c_str() );
3737
}
3838
}
3939

4040
void Tracker::AddResult( GameMode mode, Outcome outcome, GoingOrder order, Class ownClass, Class opponentClass,
4141
const CardHistoryList& historyCardList, int durationInSeconds, int rank, int legend )
4242
{
4343
if( mode == MODE_SOLO_ADVENTURES ) {
44-
LOG( "Ignore solo adventure" );
44+
INFO( "Ignore solo adventure" );
4545
return;
4646
}
4747

4848
if( mode == MODE_TAVERN_BRAWL ) {
49-
LOG( "Ignore tavern brawl" );
49+
INFO( "Ignore tavern brawl" );
5050
return;
5151
}
5252

53-
#ifdef _DEBUG
54-
string cardHistoryOutput;
55-
for( CardHistoryList::const_iterator it = historyCardList.begin(); it != historyCardList.end(); ++it ) {
56-
cardHistoryOutput += (*it).player == PLAYER_SELF ? "SELF " : "OPPONENT ";
57-
cardHistoryOutput += (*it).cardId + "\n";
58-
}
59-
DEBUG( "Card History: %s", cardHistoryOutput.c_str() );
60-
#endif
61-
6253
if( outcome == OUTCOME_UNKNOWN ) {
6354
mUnknownOutcomeCount++;
64-
LOG( "Outcome unknown. Skip result" );
55+
INFO( "Outcome unknown. Skip result" );
6556
return;
6657
}
6758

6859
if( mode == MODE_UNKNOWN ) {
6960
mUnknownModeCount++;
70-
LOG( "Mode unknown. Skip result" );
61+
INFO( "Mode unknown. Skip result" );
7162
return;
7263
}
7364

7465
if( order == ORDER_UNKNOWN ) {
7566
mUnknownOrderCount++;
76-
LOG( "Order unknown. Skip result" );
67+
INFO( "Order unknown. Skip result" );
7768
return;
7869
}
7970

8071
if( ownClass == CLASS_UNKNOWN ) {
8172
mUnknownClassCount++;
82-
LOG( "Own Class unknown. Skip result" );
73+
INFO( "Own Class unknown. Skip result" );
8374
return;
8475
}
8576

8677
if( opponentClass == CLASS_UNKNOWN ) {
8778
mUnknownOpponentCount++;
88-
LOG( "Class of Opponent unknown. Skip result" );
79+
INFO( "Class of Opponent unknown. Skip result" );
8980
return;
9081
}
9182

9283
mSuccessfulResultCount++;
9384

94-
LOG( "Upload %s %s vs. %s as %s. Went %s",
85+
INFO( "Upload %s %s vs. %s as %s. Went %s",
9586
MODE_NAMES[ mode ],
9687
OUTCOME_NAMES[ outcome ],
9788
CLASS_NAMES[ opponentClass ],
@@ -167,10 +158,10 @@ QNetworkRequest Tracker::CreateTrackerRequest( const QString& path ) {
167158
void Tracker::AddResultHandleReply() {
168159
QNetworkReply *reply = static_cast< QNetworkReply* >( sender() );
169160
if( reply->error() == QNetworkReply::NoError ) {
170-
LOG( "Result was uploaded successfully!" );
161+
INFO( "Result was uploaded successfully!" );
171162
} else {
172163
int statusCode = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute ).toInt();
173-
LOG( "There was a problem uploading the result. Error: %i HTTP Status Code: %i", reply->error(), statusCode );
164+
ERROR( "There was a problem uploading the result. Error: %i HTTP Status Code: %i", reply->error(), statusCode );
174165
}
175166
}
176167

@@ -183,17 +174,17 @@ void Tracker::CreateAndStoreAccount() {
183174
void Tracker::CreateAndStoreAccountHandleReply() {
184175
QNetworkReply *reply = static_cast< QNetworkReply* >( sender() );
185176
if( reply->error() == QNetworkReply::NoError ) {
186-
LOG( "Account creation was successful!" );
177+
INFO( "Account creation was successful!" );
187178

188179
QByteArray jsonData = reply->readAll();
189180

190181
bool ok;
191182
QtJson::JsonObject user = QtJson::parse( jsonData, ok ).toMap();
192183

193184
if( !ok ) {
194-
LOG( "Couldn't parse response" );
185+
ERROR( "Couldn't parse response" );
195186
} else {
196-
LOG( "Welcome %s", user[ "username" ].toString().toStdString().c_str() );
187+
INFO( "Welcome %s", user[ "username" ].toString().toStdString().c_str() );
197188

198189
SetUsername( user["username"].toString() );
199190
SetPassword( user["password"].toString() );
@@ -202,7 +193,7 @@ void Tracker::CreateAndStoreAccountHandleReply() {
202193
}
203194
} else {
204195
int statusCode = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute ).toInt();
205-
LOG( "There was a problem creating an account. Error: %i HTTP Status Code: %i", reply->error(), statusCode );
196+
ERROR( "There was a problem creating an account. Error: %i HTTP Status Code: %i", reply->error(), statusCode );
206197
}
207198
}
208199

@@ -220,14 +211,14 @@ void Tracker::OpenProfileHandleReply() {
220211
QtJson::JsonObject response = QtJson::parse( jsonData, ok ).toMap();
221212

222213
if( !ok ) {
223-
LOG( "Couldn't parse response" );
214+
INFO( "Couldn't parse response" );
224215
} else {
225216
QString url = response[ "url" ].toString();
226217
QDesktopServices::openUrl( QUrl( url ) );
227218
}
228219
} else {
229220
int statusCode = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute ).toInt();
230-
LOG( "There was a problem creating an auth token. Error: %i HTTP Status Code: %i", reply->error(), statusCode );
221+
ERROR( "There was a problem creating an auth token. Error: %i HTTP Status Code: %i", reply->error(), statusCode );
231222
}
232223
}
233224

src/Window.cpp

+21-8
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void SettingsTab::ExportAccount() {
5151
out << Tracker::Instance()->Password();
5252
out << Tracker::Instance()->WebserviceURL();
5353

54-
LOG( "Account %s exported in %s", Tracker::Instance()->Username().toStdString().c_str(), fileName.toStdString().c_str() );
54+
INFO( "Account %s exported in %s", Tracker::Instance()->Username().toStdString().c_str(), fileName.toStdString().c_str() );
5555
}
5656
}
5757

@@ -87,10 +87,10 @@ void SettingsTab::ImportAccount() {
8787
Tracker::Instance()->SetPassword( password );
8888
Tracker::Instance()->SetWebserviceURL( webserviceUrl );
8989

90-
LOG( "Account %s imported from %s", username.toStdString().c_str(), fileName.toStdString().c_str() );
90+
INFO( "Account %s imported from %s", username.toStdString().c_str(), fileName.toStdString().c_str() );
9191
LoadSettings();
9292
} else {
93-
LOG( "Import failed" );
93+
ERROR( "Import failed" );
9494
}
9595
}
9696
}
@@ -128,19 +128,32 @@ LogTab::LogTab( QWidget *parent )
128128
{
129129
mUI->setupUi( this );
130130

131-
QFont font( "Monospace" );
132-
font.setStyleHint( QFont::TypeWriter );
133-
mUI->logText->setFont( font );
131+
QFont fixedFont = QFontDatabase::systemFont( QFontDatabase::FixedFont );
132+
mUI->logText->setFont( fixedFont );
134133

135-
connect( Logger::Instance(), SIGNAL( NewMessage(const string&) ), this, SLOT( AddLogEntry(const string&) ) );
134+
connect( Logger::Instance(), SIGNAL( NewMessage(LogEventType, const string&) ), this, SLOT( AddLogEntry(LogEventType, const string&) ) );
136135
}
137136

138137
LogTab::~LogTab() {
139138
delete mUI;
140139
}
141140

142-
void LogTab::AddLogEntry( const string& msg ) {
141+
void LogTab::AddLogEntry( LogEventType type, const string& msg ) {
143142
mUI->logText->moveCursor( QTextCursor::End );
143+
144+
switch( type ) {
145+
case LOG_ERROR:
146+
mUI->logText->setTextColor( Qt::red );
147+
break;
148+
149+
case LOG_DEBUG:
150+
mUI->logText->setTextColor( Qt::gray );
151+
break;
152+
153+
default:
154+
mUI->logText->setTextColor( QApplication::palette().text().color() );
155+
}
156+
144157
mUI->logText->insertPlainText( msg.c_str() );
145158
mUI->logText->moveCursor( QTextCursor::End );
146159
}

src/Window.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class LogTab : public QWidget
5757
Ui::LogWidget *mUI;
5858

5959
private slots:
60-
void AddLogEntry( const string& message );
60+
void AddLogEntry( LogEventType type, const string& message );
6161

6262
public:
6363
explicit LogTab( QWidget *parent = 0 );

0 commit comments

Comments
 (0)