1 /*****************************************************************************
2  * PokerTH - The open source texas holdem engine                             *
3  * Copyright (C) 2006-2012 Felix Hammer, Florian Thauer, Lothar May          *
4  *                                                                           *
5  * This program is free software: you can redistribute it and/or modify      *
6  * it under the terms of the GNU Affero General Public License as            *
7  * published by the Free Software Foundation, either version 3 of the        *
8  * License, or (at your option) any later version.                           *
9  *                                                                           *
10  * This program is distributed in the hope that it will be useful,           *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
13  * GNU Affero General Public License for more details.                       *
14  *                                                                           *
15  * You should have received a copy of the GNU Affero General Public License  *
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.     *
17  *                                                                           *
18  *                                                                           *
19  * Additional permission under GNU AGPL version 3 section 7                  *
20  *                                                                           *
21  * If you modify this program, or any covered work, by linking or            *
22  * combining it with the OpenSSL project's OpenSSL library (or a             *
23  * modified version of that library), containing parts covered by the        *
24  * terms of the OpenSSL or SSLeay licenses, the authors of PokerTH           *
25  * (Felix Hammer, Florian Thauer, Lothar May) grant you additional           *
26  * permission to convey the resulting work.                                  *
27  * Corresponding Source for a non-source form of such a combination          *
28  * shall include the source code for the parts of OpenSSL used as well       *
29  * as that of the covered work.                                              *
30  *****************************************************************************/
31 
32 #ifndef POKERTH_DEDICATED_SERVER
33 #error This file is only for the server.
34 #endif
35 
36 #ifdef _MSC_VER
37 #pragma warning(push)
38 #pragma warning(disable : 4100)
39 #endif
40 
41 #include <core/loghelper.h>
42 #include <fstream>
43 #include <boost/filesystem.hpp>
44 #include <boost/date_time.hpp>
45 
46 
47 using namespace std;
48 using namespace boost::filesystem;
49 using namespace boost::posix_time;
50 
51 #define SERVER_MSG_LOG_FILE_NAME				"server_messages.log"
52 
53 static string g_logFile;
54 static int g_logLevel = 1;
55 
56 void
loghelper_init(const string & logDir,int logLevel)57 loghelper_init(const string &logDir, int logLevel)
58 {
59 	path tmpLogFile(logDir);
60 	tmpLogFile /= SERVER_MSG_LOG_FILE_NAME;
61 
62 	g_logFile = tmpLogFile.directory_string();
63 	g_logLevel = logLevel;
64 }
65 
66 void
internal_log_err(const string & msg)67 internal_log_err(const string &msg)
68 {
69 	if (!g_logFile.empty()) {
70 		std::ofstream o(g_logFile.c_str(), ios_base::out | ios_base::app);
71 		if (!o.fail()) {
72 			o << second_clock::local_time() << " ERR: " << msg;
73 			o.flush();
74 		}
75 	}
76 }
77 
78 void
internal_log_msg(const std::string & msg)79 internal_log_msg(const std::string &msg)
80 {
81 	if (g_logLevel) {
82 		if (!g_logFile.empty()) {
83 			std::ofstream o(g_logFile.c_str(), ios_base::out | ios_base::app);
84 			if (!o.fail())
85 				o << second_clock::local_time() << " MSG: " << msg;
86 		}
87 	}
88 }
89 
90 void
internal_log_level(const std::string & msg,int logLevel)91 internal_log_level(const std::string &msg, int logLevel)
92 {
93 	if (g_logLevel >= logLevel) {
94 		if (!g_logFile.empty()) {
95 			std::ofstream o(g_logFile.c_str(), ios_base::out | ios_base::app);
96 			if (!o.fail())
97 				o << second_clock::local_time() << " OUT: " << msg;
98 		}
99 	}
100 }
101 
102 #ifdef _MSC_VER
103 #pragma warning(pop)
104 #endif
105 
106