blob: ac249b39d216e47497798d09e6bfb28dada86974 [file] [log] [blame]
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +00001/* Copyright (C) 2002-2004 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19// -=- Logger_file.cxx - Logger instance for a file
20
21#include <stdlib.h>
22#include <string.h>
23
24#include <rfb/util.h>
25#include <rfb/Logger_file.h>
26#include <rfb/Threading.h>
27
28using namespace rfb;
29
30
31// If threading is available then protect the write() operation
32// from concurrent accesses
33#ifdef __RFB_THREADING_IMPL
34static Mutex logLock;
35#endif
36
37
38Logger_File::Logger_File(const char* loggerName)
39 : Logger(loggerName), indent(13), width(79), m_filename(0), m_file(0),
40 m_lastLogTime(0)
41{
42}
43
44Logger_File::~Logger_File()
45{
46 closeFile();
47}
48
49void Logger_File::write(int level, const char *logname, const char *message)
50{
51#ifdef __RFB_THREADING_IMPL
52 Lock l(logLock);
53#endif
54 if (!m_file) {
55 if (!m_filename) return;
56 m_file = fopen(m_filename, "w+");
57 if (!m_file) return;
58 }
59
60#ifndef _WIN32_WCE
61 time_t current = time(0);
62 if (current != m_lastLogTime) {
63 m_lastLogTime = current;
64 fprintf(m_file, "\n%s", ctime(&m_lastLogTime));
65 }
66#endif
67
68 fprintf(m_file," %s:", logname);
69 int column = strlen(logname) + 2;
70 if (column < indent) {
71 fprintf(m_file,"%*s",indent-column,"");
72 column = indent;
73 }
74 while (true) {
75 const char* s = strchr(message, ' ');
76 int wordLen;
77 if (s) wordLen = s-message;
78 else wordLen = strlen(message);
79
80 if (column + wordLen + 1 > width) {
81 fprintf(m_file,"\n%*s",indent,"");
82 column = indent;
83 }
84 fprintf(m_file," %.*s",wordLen,message);
85 column += wordLen + 1;
86 message += wordLen + 1;
87 if (!s) break;
88 }
89 fprintf(m_file,"\n");
90 fflush(m_file);
91}
92
93void Logger_File::setFilename(const char* filename)
94{
95 closeFile();
96 m_filename = strDup(filename);
97}
98
99void Logger_File::setFile(FILE* file)
100{
101 closeFile();
102 m_file = file;
103}
104
105void Logger_File::closeFile()
106{
107 if (m_filename) {
108 if (m_file) {
109 fclose(m_file);
110 m_file = 0;
111 }
112 strFree(m_filename);
113 m_filename = 0;
114 }
115}
116
117static Logger_File logger("file");
118
119bool rfb::initFileLogger(const char* filename) {
120 logger.setFilename(filename);
121 logger.registerLogger();
122 return true;
123}