1 /*
2 Copyright (C) 2009-2010 wxLauncher Team
3 
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 */
18 
19 #include <wx/wx.h>
20 #include <wx/wfstream.h>
21 #include <wx/datetime.h>
22 #include <wx/filename.h>
23 #include <wx/stdpaths.h>
24 
25 #include <wchar.h>
26 
27 #include "controls/Logger.h"
28 #include "controls/StatusBar.h"
29 
30 #include "global/MemoryDebugging.h"
31 
32 ////// Logger
33 const wxString levels[] = {
34 	wxT_2("FATAL"),
35 	wxT_2("ERROR"),
36 	wxT_2("WARN "),
37 	wxT_2("MSG  "),
38 	wxT_2("STSBR"),
39 	wxT_2("INFO "),
40 	wxT_2("DEBUG"),
41 };
42 /** Constructor. */
Logger()43 Logger::Logger() {
44 	wxFileName outFileName(wxStandardPaths::Get().GetUserDataDir(), wxT_2("wxLauncher.log"));
45 	if (!outFileName.DirExists() &&
46 		!wxFileName::Mkdir(outFileName.GetPath(), 0700, wxPATH_MKDIR_FULL) ) {
47 			wxLogFatalError(_("Unable to create folder to place log in. (%s)"), outFileName.GetPath().c_str());
48 	}
49 
50 	this->outFile = new wxFFile(outFileName.GetFullPath(), wxT_2("wb"));
51 	if (!outFile->IsOpened()) {
52 		wxLogFatalError(_("Unable to open log output file. (%s)"), outFileName.GetFullPath().c_str());
53 	}
54 	this->out = new wxFFileOutputStream(*outFile);
55 	wxASSERT_MSG(out->IsOk(), wxT_2("Log output file is not valid!"));
56 	this->out->Write("\357\273\277", 3);
57 
58 	this->statusBar = NULL;
59 }
60 
61 /** Destructor. */
~Logger()62 Logger::~Logger() {
63 	char exitmsg[] = "\nLog closed.\n";
64 	this->out->Write(exitmsg, strlen(exitmsg));
65 	this->out->Close();
66 	delete this->out;
67 	delete this->outFile;
68 }
69 
70 /** Overridden as per wxWidgets docs to implement a wxLog. */
71 /* Compatiblity with 2.8.x */
72 #if wxMAJOR_VERSION == 2 && wxMINOR_VERSION >= 8
DoLog(wxLogLevel level,const wxChar * msg,time_t time)73 void Logger::DoLog(
74 		wxLogLevel level,
75 		const wxChar *msg,
76 		time_t time)
77 {
78 	wxString timestr = wxDateTime(time).Format(
79 		wxT_2("%y%j%H%M%S"),
80 		wxDateTime::GMT0);
81 #else
82 void Logger::DoLogRecord(
83 	wxLogLevel level,
84 	const wxString& msg,
85 	const wxLogRecordInfo& info)
86 {
87 	wxString timestr = wxDateTime(info.timestamp).Format(
88 		wxT_2("%y%j%H%M%S"),
89 		wxDateTime::GMT0);
90 #endif
91 	wxString str = wxString::Format(
92     wxT_2("%s:%s:"), timestr.c_str(), levels[level].c_str());
93 	wxString buf(msg);
94 	out->Write(str.mb_str(wxConvUTF8), str.size());
95 	out->Write(buf.mb_str(wxConvUTF8), buf.size());
96 	out->Write("\n", 1);
97 
98 	if ( this->statusBar != NULL ) {
99 		if ( level == 1 ) { // error
100 			this->statusBar->SetMainStatusText(buf, ID_SB_ERROR);
101 		} else if ( level == 2 ) { // warning
102 			this->statusBar->SetMainStatusText(buf, ID_SB_WARNING);
103 		} else if ( level == 3 || level == 4 ) { // message, statubar
104 			this->statusBar->SetMainStatusText(buf, ID_SB_OK);
105 		} else if ( level == 5 ) { // info
106 			this->statusBar->SetMainStatusText(buf, ID_SB_INFO);
107 		}
108 	}
109 }
110 
111 void Logger::Flush() {
112 	outFile->Flush(); // Warning: ignoring return value from Flush().
113 }
114 
115 /** Stores the pointer the status bar that I am to send status messages to.
116 If a status bar is already set, function will do nothing to the old statusbar.
117 Logger does not take over managment of the statusbar passed in. */
118 void Logger::SetStatusBarTarget(StatusBar *bar) {
119 	this->statusBar = bar;
120 }
121 
122