|
1 /* |
|
2 * Copyright (c) 2010 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 |
|
18 #include <QDir> |
|
19 #include <QApplication> |
|
20 #include <QDateTime> |
|
21 #include "qstmfilelogger.h" |
|
22 |
|
23 QStm_FileLogger* QStm_FileLogger::m_instance = 0; |
|
24 |
|
25 QStm_FileLogger::QStm_FileLogger() |
|
26 { |
|
27 QString fname = getLogFileName(); |
|
28 m_file = new QFile(fname); |
|
29 m_file->open(QIODevice::ReadWrite | QIODevice::Text); |
|
30 m_stream = new QTextStream(m_file); |
|
31 } |
|
32 |
|
33 |
|
34 QStm_FileLogger::~QStm_FileLogger() |
|
35 { |
|
36 delete m_stream; |
|
37 m_file->close(); |
|
38 delete m_file; |
|
39 } |
|
40 |
|
41 QString QStm_FileLogger::getLogFileName() |
|
42 { |
|
43 #ifdef Q_OS_SYMBIAN |
|
44 QString path = "E:/Others/Logs"; |
|
45 QDir logdir(path); |
|
46 logdir.mkpath(path); |
|
47 #else |
|
48 QString path = QDir::tempPath(); |
|
49 #endif |
|
50 |
|
51 QString appName = qApp->applicationFilePath(); |
|
52 QFileInfo fi(appName); |
|
53 QString baseName = fi.baseName(); |
|
54 QString fileName = path % "/" % baseName % |
|
55 QDateTime::currentDateTime().toString("ddMMyy_hhmmss") % |
|
56 ".log"; |
|
57 return fileName; |
|
58 } |
|
59 |
|
60 |
|
61 void QStm_FileLogger::log(const char* fmt,...) |
|
62 { |
|
63 va_list list; |
|
64 QString logStr; |
|
65 va_start(list, fmt); |
|
66 logStr.vsprintf(fmt, list); |
|
67 logger()->doLog(logStr); |
|
68 va_end(list); |
|
69 } |
|
70 |
|
71 |
|
72 void QStm_FileLogger::log(const QString& text) |
|
73 { |
|
74 logger()->doLog(text); |
|
75 } |
|
76 |
|
77 void QStm_FileLogger::doLog(const QString& text) |
|
78 { |
|
79 *m_stream << QDateTime::currentDateTime().toString("dd-MM-yy hh:mm:ss.zzz") << " " << |
|
80 text << "\n"; |
|
81 } |
|
82 |
|
83 void QStm_FileLogger::doLog(const char* text) |
|
84 { |
|
85 *m_stream << QDateTime::currentDateTime().toString("dd-MM-yy hh:mm:ss.zzz") << " " << |
|
86 text << "\n"; |
|
87 } |
|
88 |
|
89 QStm_FileLogger* QStm_FileLogger::logger() |
|
90 { |
|
91 if (!m_instance) { |
|
92 m_instance = new QStm_FileLogger(); |
|
93 } |
|
94 return m_instance; |
|
95 } |