logging.cpp

Go to the documentation of this file.
00001 // !concept {:name => "Portable logging"}
00002 
00003 #include "common/logging.h"
00004 
00005 #if __DO_LOGGING__
00006 
00007 #if defined(__EPOC32__)
00008 
00009 #include <f32file.h> // RFs
00010 #include <flogger.h> // flogger.lib required, Symbian 7.0-up
00011 
00012 DEFINE_LOG_FILE_DIR; // KLogFileDir _LIT definition
00013 DEFINE_ASSERT_LOG_FILENAME; // KAssertLogFile _LIT definition
00014 
00015 extern "C"
00016 void log_clear(const char *logfile)
00017 {
00018   // We had some trouble with higher-level APIs on some Symbian
00019   // versions. This certainly should "truncate" the file on any
00020   // Symbian version.
00021   RFs fs;
00022   if (fs.Connect() == KErrNone) {
00023     TFileName wLogFile;
00024     _LIT(KLogPath, "c:\\logs\\");
00025     _LIT(KSlash, "\\");
00026     wLogFile.Copy(KLogPath);
00027     wLogFile.Append(KLogFileDir);
00028     wLogFile.Append(KSlash);
00029     TBuf<32> buf;
00030     buf.Copy(TPtrC8(reinterpret_cast<const TUint8*>(logfile)));
00031     wLogFile.Append(buf);
00032     TInt errCode = fs.Delete(wLogFile);
00033     fs.Close();
00034 
00035     if (errCode && (errCode != KErrNotFound)) {
00036       if (errCode == KErrInUse)
00037         // If you get this, make sure you do not log anything _before_
00038         // calling log_clear.
00039   log_fmt(logfile, "logfile truncation failed: in use (%d)", errCode);
00040       else
00041   log_fmt(logfile, "logfile truncation failed: %d", errCode);
00042     }
00043   }
00044 }
00045 
00046 extern "C"
00047 void log_text(const char *logfile, const char *s)
00048 {
00049   TFileName wLogFile;
00050   wLogFile.Copy(TPtrC8(reinterpret_cast<const TUint8*>(logfile)));
00051   RFileLogger::Write(KLogFileDir, wLogFile, EFileLoggingModeAppend, TPtrC8(reinterpret_cast<const TUint8*>(s)));
00052 }
00053 
00054 _LIT8(KCtxFmt, "func %s, file %s, line %d: %s");
00055 
00056 extern "C"
00057 void __log_ctx(const char *logfile, const char *func, const char *file, int line, const char *s)
00058 {
00059   TFileName wLogFile;
00060   wLogFile.Copy(TPtrC8(reinterpret_cast<const TUint8*>(logfile)));
00061   RFileLogger::WriteFormat(KLogFileDir, wLogFile, EFileLoggingModeAppend, KCtxFmt, func, file, line, s);
00062 }
00063 
00064 extern "C"
00065 void log_fmt(const char* logfile, const char* fmt, ...)
00066 {
00067   VA_LIST argp;
00068   VA_START(argp, fmt);
00069 
00070   TFileName wLogFile;
00071   wLogFile.Copy(TPtrC8(reinterpret_cast<const TUint8*>(logfile)));
00072 
00073   RFileLogger::WriteFormat(KLogFileDir, wLogFile, EFileLoggingModeAppend, TPtrC8(reinterpret_cast<const TUint8*>(fmt)), argp);
00074 }
00075 
00076 #ifndef NDEBUG
00077 extern "C"
00078 void epoc_log_assert(const char *func, const char *file, int line, const char *s)
00079 {
00080   RFileLogger::WriteFormat(KLogFileDir, KAssertLogFile, EFileLoggingModeOverwrite, KCtxFmt, func, file, line, s);
00081 }
00082 #endif
00083 
00084 #else // not Symbian
00085 
00086 #include <stdio.h>
00087 #include <stdarg.h> // va_list
00088 
00089 extern "C"
00090 void log_text(const char *logfile, const char *s)
00091 {
00092   printf("[%s] %s\n", logfile, s);
00093 }
00094 
00095 extern "C"
00096 void __log_ctx(const char *logfile, const char *func, const char *file, int line, const char *s)
00097 {
00098   printf("[%s] func %s, file %s, line %d: %s\n", logfile, func, file, line, s);
00099 }
00100 
00101 extern "C"
00102 void log_fmt(const char* logfile, const char* fmt, ...)
00103 {
00104   va_list argp;
00105   va_start(argp, fmt);
00106   printf("[%s] ", logfile);
00107   vprintf(fmt, argp);
00108   printf("\n");
00109   va_end(argp); // any cleanup
00110 }
00111 
00112 #endif // not Symbian
00113 #endif // no logging
00114 
00115 /**
00116 
00117 logging.cpp
00118 
00119 Copyright 2009 Helsinki Institute for Information Technology (HIIT)
00120 and the authors. All rights reserved.
00121 
00122 Authors: Tero Hasu <tero.hasu@hut.fi>
00123 
00124 Permission is hereby granted, free of charge, to any person
00125 obtaining a copy of this software and associated documentation files
00126 (the "Software"), to deal in the Software without restriction,
00127 including without limitation the rights to use, copy, modify, merge,
00128 publish, distribute, sublicense, and/or sell copies of the Software,
00129 and to permit persons to whom the Software is furnished to do so,
00130 subject to the following conditions:
00131 
00132 The above copyright notice and this permission notice shall be
00133 included in all copies or substantial portions of the Software.
00134 
00135 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00136 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00137 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00138 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
00139 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
00140 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00141 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00142 SOFTWARE.
00143 
00144  **/

ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:54 2011 by Doxygen 1.6.1