1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0"" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 #include "logoutputimpl.h" |
|
18 #include <QFile> |
|
19 #include <QDir> |
|
20 #include <QFileInfo> |
|
21 #include <QTextStream> |
|
22 #include <QSettings> |
|
23 #include "logger.h" |
|
24 #include "loglogger.h" |
|
25 #include "logoutputfactory.h" |
|
26 |
|
27 #ifdef Q_WS_WIN |
|
28 #include <Windows.h> |
|
29 #endif |
|
30 |
|
31 //DebugLogOutput |
|
32 REGISTER_OUTPUT_LOG(DEBUGOUTPUT_NAME,DebugLogOutput) |
|
33 |
|
34 DebugLogOutput::DebugLogOutput() |
|
35 { |
|
36 } |
|
37 |
|
38 DebugLogOutput::~DebugLogOutput() |
|
39 { |
|
40 } |
|
41 |
|
42 void DebugLogOutput::output(const QString &log) |
|
43 { |
|
44 #ifdef Q_WS_WIN |
|
45 QT_WA({ |
|
46 OutputDebugStringW(reinterpret_cast<const WCHAR*>(log.utf16())); |
|
47 }, { |
|
48 OutputDebugStringA(log.toLocal8Bit().data()); |
|
49 }); |
|
50 #else |
|
51 fprintf(stderr, log.toLocal8Bit().data()); |
|
52 fflush(stderr); |
|
53 #endif |
|
54 } |
|
55 |
|
56 bool DebugLogOutput::init() |
|
57 { |
|
58 return true; |
|
59 } |
|
60 |
|
61 |
|
62 bool DebugLogOutput::doLoad(QSettings &settings) |
|
63 { |
|
64 Q_UNUSED(settings); |
|
65 return true; |
|
66 } |
|
67 |
|
68 |
|
69 //ConsoleLogOutput |
|
70 |
|
71 REGISTER_OUTPUT_LOG(CONSOLEOUTPUT_NAME,ConsoleLogOutput) |
|
72 |
|
73 ConsoleLogOutput::ConsoleLogOutput() |
|
74 { |
|
75 } |
|
76 |
|
77 ConsoleLogOutput::~ConsoleLogOutput() |
|
78 { |
|
79 } |
|
80 |
|
81 void ConsoleLogOutput::output(const QString &log) |
|
82 { |
|
83 printf(log.toLocal8Bit().data()); |
|
84 } |
|
85 |
|
86 bool ConsoleLogOutput::init() |
|
87 { |
|
88 return true; |
|
89 } |
|
90 |
|
91 |
|
92 bool ConsoleLogOutput::doLoad(QSettings &settings) |
|
93 { |
|
94 Q_UNUSED(settings); |
|
95 return true; |
|
96 } |
|
97 |
|
98 //FileLogOutput |
|
99 |
|
100 REGISTER_OUTPUT_LOG(FILEOUTPUT_NAME,FileLogOutput) |
|
101 |
|
102 FileLogOutput::FileLogOutput(const QString &logFilePath /*= QString()*/) : |
|
103 mLogFile(0),mTextStream(0),mLogFilePath(logFilePath),mTruncateFile(false) |
|
104 { |
|
105 } |
|
106 |
|
107 FileLogOutput::~FileLogOutput() |
|
108 { |
|
109 delete mTextStream; |
|
110 delete mLogFile; |
|
111 } |
|
112 |
|
113 void FileLogOutput::output(const QString &log) |
|
114 { |
|
115 *mTextStream << log; |
|
116 mTextStream->flush(); |
|
117 } |
|
118 |
|
119 bool FileLogOutput::init() |
|
120 { |
|
121 if (mTextStream) { |
|
122 delete mTextStream; |
|
123 mTextStream = 0; |
|
124 } |
|
125 |
|
126 if (mLogFile) { |
|
127 delete mLogFile; |
|
128 mLogFile = 0; |
|
129 } |
|
130 |
|
131 QFileInfo fileInfo(mLogFilePath); |
|
132 QDir parentDir = fileInfo.dir(); |
|
133 if (!parentDir.exists()) |
|
134 { |
|
135 QString path = parentDir.absolutePath(); |
|
136 parentDir.cdUp(); |
|
137 parentDir.mkpath(path); |
|
138 } |
|
139 |
|
140 mLogFile = new QFile(mLogFilePath); |
|
141 QFile::OpenMode mode = QIODevice::WriteOnly | QIODevice::Text; |
|
142 |
|
143 if (mTruncateFile) { |
|
144 mode |= QIODevice::Truncate; |
|
145 } |
|
146 else { |
|
147 mode |= QIODevice::Append; |
|
148 } |
|
149 |
|
150 if (!mLogFile->open(mode)) |
|
151 { |
|
152 LOGGER_LOG(QString("FileLogOutput::init << Open open file failed: ") + mLogFilePath); |
|
153 return false; |
|
154 } |
|
155 mTextStream = new QTextStream(mLogFile); |
|
156 return true; |
|
157 } |
|
158 |
|
159 bool FileLogOutput::doLoad(QSettings &settings) |
|
160 { |
|
161 if (!parentLogger()) { |
|
162 return false; |
|
163 } |
|
164 |
|
165 settings.beginGroup(parentLogger()->name()); |
|
166 mLogFilePath = settings.value( |
|
167 FILEOUTPUT_NAME + SETTINGS_SECTION_SEPERATOR + FILEOUTPUT_LOGFILE).toString(); |
|
168 mTruncateFile = settings.value( |
|
169 FILEOUTPUT_NAME + SETTINGS_SECTION_SEPERATOR + FILEOUTPUT_TRUNCATE,QVariant(false)).toBool(); |
|
170 settings.endGroup(); |
|
171 |
|
172 if (mLogFilePath.isEmpty()) { |
|
173 return false; |
|
174 } |
|
175 |
|
176 return true; |
|
177 } |
|
178 |
|
179 bool FileLogOutput::truncate() const |
|
180 { |
|
181 return mTruncateFile; |
|
182 } |
|
183 |
|
184 void FileLogOutput::setTruncate(bool truncate) |
|
185 { |
|
186 mTruncateFile = truncate; |
|
187 } |
|
188 |
|
189 QString FileLogOutput::logFile() const |
|
190 { |
|
191 return mLogFilePath; |
|
192 } |
|
193 |
|
194 void FileLogOutput::setLogFile(const QString &logFile) |
|
195 { |
|
196 mLogFilePath = logFile; |
|
197 } |
|
198 |
|