emailservices/emaildebug/inc/emailtrace.h
changeset 75 47d84de1c893
parent 59 16ed8d08d0b1
child 74 6c59112cfd31
equal deleted inserted replaced
72:64e38f08e49c 75:47d84de1c893
    17 
    17 
    18 #ifndef EMAILTRACE_H
    18 #ifndef EMAILTRACE_H
    19 #define EMAILTRACE_H
    19 #define EMAILTRACE_H
    20 
    20 
    21 #include <QCoreApplication>
    21 #include <QCoreApplication>
       
    22 #include <QDateTime>
    22 #include <QDebug>
    23 #include <QDebug>
    23 #include <QFile>
    24 #include <QFile>
    24 #include <QThread>
    25 #include <QTextStream>
    25 #include <QDateTime>
    26 #include <QAbstractFileEngine>
    26 
    27 
    27 /*
    28 /*
    28  * The macros NM_COMMENT_TRACES, NM_ERROR_TRACES, and NM_FUNCTION_TRACES
    29  * The macros NM_COMMENT_TRACES, NM_ERROR_TRACES, NM_FUNCTION_TRACES, and
    29  * control which debug messages are printed. The trace logging is controlled
    30  * NM_TIMESTAMP_TRACES control which debug messages are printed. The trace
    30  * with the NM_LOG_TO_FILE macro, whereas the NM_LOG_DIRECTORY macro defines
    31  * logging is controlled with the NM_LOG_TO_FILE macro, whereas the
    31  * which directory is to be used to store the log files. The print_trace()
    32  * NM_LOG_DIRECTORY macro defines which directory is to be used to store the
    32  * helper function implements printing. Log files are named according to
    33  * log files. The print_trace() helper function implements printing. Log
    33  * process and thread IDs. If NM_LOG_TO_FILE is zero or a log file cannot be
    34  * files are named according to process thread IDs. If NM_LOG_TO_FILE is zero
    34  * opened, the messages are printed to qDebug(). The DSC2STR() function can
    35  * or a log file cannot be opened, the messages are printed to qDebug(). The
    35  * be used to convert Symbian descriptors to QString objects.
    36  * DSC2STR() function can be used to convert Symbian descriptors to QString
    36  */
    37  * objects.
    37 #if defined(DEBUG) || defined(_DEBUG) || NM_COMMENT_TRACES || NM_ERROR_TRACES || NM_FUNCTION_TRACES
    38  * 
       
    39  * NOTICE that you can control the tracing system for a component tree by
       
    40  * using qmake. For example, if you only want to enable timestamps for a
       
    41  * component, you can invoke
       
    42  * 
       
    43  *     qmake "DEFINES += DEBUG NM_TIMESTAMP_TRACES=1" -r
       
    44  * 
       
    45  * before compiling the component. You can add as many macro definitions
       
    46  * between the quotation marks as you wish. For example, if you want to log
       
    47  * the trace messages to "c:/logs", add the definitions
       
    48  * 
       
    49  *     NM_LOG_TO_FILE=1 NM_LOG_DIRECTORY=\\\"c:/logs/\\\"
       
    50  * 
       
    51  * between the quotation marks. Do not forget to add the terminating slash
       
    52  * character and notice how the quotation marks around the directory path
       
    53  * have been escaped. This does not work if the log directory does not exist.
       
    54  */
       
    55 
       
    56 // Timestamp traces always ON
       
    57 #ifndef NM_TIMESTAMP_TRACES
       
    58 #define NM_TIMESTAMP_TRACES 1
       
    59 #endif
       
    60 
       
    61 #ifndef NM_LOG_TO_FILE
       
    62 #define NM_LOG_TO_FILE   1
       
    63 #endif
       
    64 #ifndef NM_LOG_DIRECTORY
       
    65 #define NM_LOG_DIRECTORY "c:/data/logs/"
       
    66 #endif
       
    67 
       
    68 #if defined(DEBUG) || defined(_DEBUG)
    38 
    69 
    39 #ifndef NM_COMMENT_TRACES
    70 #ifndef NM_COMMENT_TRACES
    40 #define NM_COMMENT_TRACES  0
    71 #define NM_COMMENT_TRACES   0
    41 #endif
    72 #endif
    42 
       
    43 #ifndef NM_ERROR_TRACES
    73 #ifndef NM_ERROR_TRACES
    44 #define NM_ERROR_TRACES    0
    74 #define NM_ERROR_TRACES     0
    45 #endif
    75 #endif
    46 
       
    47 #ifndef NM_FUNCTION_TRACES
    76 #ifndef NM_FUNCTION_TRACES
    48 #define NM_FUNCTION_TRACES 0
    77 #define NM_FUNCTION_TRACES  0
    49 #endif
    78 #endif
    50 
    79 
    51 #if NM_COMMENT_TRACES || NM_ERROR_TRACES || NM_FUNCTION_TRACES
    80 #if NM_COMMENT_TRACES || NM_ERROR_TRACES || NM_FUNCTION_TRACES
    52 
    81 
    53 #define NM_LOG_TO_FILE   1
       
    54 #define NM_LOG_DIRECTORY "c:/data/logs/"
       
    55 
       
    56 inline void print_trace(const QString& msg)
    82 inline void print_trace(const QString& msg)
    57 {
    83 {
    58     static QFile file(NM_LOG_DIRECTORY+
    84     static QFile file(NM_LOG_DIRECTORY+
    59                       QString("nmail_p%1_t%2.log").
    85                       QString("nmail_p%1.log").
    60                       arg(QCoreApplication::applicationPid()).
    86                       arg(QCoreApplication::applicationPid()));
    61                       arg(QThread::currentThreadId()));
       
    62     if (NM_LOG_TO_FILE && !file.isOpen()) {
    87     if (NM_LOG_TO_FILE && !file.isOpen()) {
    63         file.open(QIODevice::Append | QIODevice::Text);
    88         file.open(QIODevice::Append | QIODevice::Text);
    64     }
    89     }
    65     QDateTime dt = QDateTime::currentDateTime ();
       
    66     if (file.isWritable()) {
    90     if (file.isWritable()) {
    67         QDebug(&file).nospace() << dt.toString(Qt::ISODate) << " " << msg << '\n';
    91         QTextStream(&file) << msg << '\n';
    68     } else {
    92     } else {
    69         qDebug().nospace() << "[Nmail] " << msg;
    93         qDebug("[Nmail] %s",msg.toLatin1().data());
    70     }
    94     }
    71 }
    95 }
    72 
    96 
    73 #endif
    97 #endif
    74 
    98 
   215 #define NM_FUNCTION
   239 #define NM_FUNCTION
   216 #define FUNC_LOG
   240 #define FUNC_LOG
   217 
   241 
   218 #endif /* NM_FUNCTION_TRACES */
   242 #endif /* NM_FUNCTION_TRACES */
   219 
   243 
       
   244 /*
       
   245  * The function NM_TIMESTAMP() prints a timestamp with a message. These traces do not
       
   246  * go to class specific log -files, but to one file called nmail_timestamps.log.
       
   247  */
       
   248 #if NM_TIMESTAMP_TRACES
       
   249 
       
   250 inline void NM_TIMESTAMP(const QString& msg)
       
   251 { 
       
   252     QString timestamp ("TIMESTAMP : " + 
       
   253                        QDateTime::currentDateTime().toUTC().toString("hh:mm:ss.zzz ") + 
       
   254                        msg);
       
   255     
       
   256     QDir directory("c:/data/");
       
   257             if (directory.exists()) {
       
   258                 directory.mkdir("logs");
       
   259             }
       
   260             
       
   261     static QFile file(NM_LOG_DIRECTORY +
       
   262                       QString("nmail_timestamps.log"));
       
   263         if (NM_LOG_TO_FILE && !file.isOpen()) {
       
   264             file.open(QIODevice::Append | QIODevice::Text | QIODevice::ReadWrite);
       
   265         }
       
   266         if (file.isWritable()) {
       
   267             QTextStream(&file) << timestamp << '\n';
       
   268         } else {
       
   269             qDebug("[Nmail] %s",timestamp.toLatin1().data());
       
   270         }
       
   271 }
       
   272 
       
   273 #else
       
   274 
       
   275 #define NM_TIMESTAMP(msg)
       
   276 
       
   277 #endif /* NM_TIMESTAMP_TRACES */
       
   278 
   220 #endif /* EMAILTRACE_H */
   279 #endif /* EMAILTRACE_H */