common/src/Logger.cpp
changeset 0 c316ab048e9d
equal deleted inserted replaced
-1:000000000000 0:c316ab048e9d
       
     1 /*
       
     2  * Name        : Logger.cpp
       
     3  * Description : 
       
     4  * Project     : This file is part of OpenMAR, an Open Mobile Augmented Reality browser
       
     5  * Website     : http://OpenMAR.org
       
     6  *
       
     7  * Copyright (c) 2010 David Caabeiro
       
     8  *
       
     9  * All rights reserved. This program and the accompanying materials are made available 
       
    10  * under the terms of the Eclipse Public License v1.0 which accompanies this 
       
    11  * distribution, and is available at http://www.eclipse.org/legal/epl-v10.html
       
    12  *
       
    13  */
       
    14 
       
    15 #include "Logger.h"
       
    16 
       
    17 #include <bautils.h>
       
    18 #include <PathInfo.h>
       
    19 
       
    20 void CLogger::Write(const TDesC8& aText)
       
    21 {
       
    22     CLogger::InstanceL().DoWrite(aText);
       
    23 }
       
    24 
       
    25 void CLogger::Write(TRefByValue<const TDesC8> aFmt, ...)
       
    26 {
       
    27     VA_LIST list;
       
    28     VA_START(list, aFmt);
       
    29 
       
    30     CLogger::InstanceL().iBuffer.FormatList(aFmt, list);
       
    31     CLogger::InstanceL().DoWrite(CLogger::InstanceL().iBuffer);
       
    32 
       
    33     VA_END(list);
       
    34 }
       
    35 
       
    36 void CLogger::Close()
       
    37 {
       
    38     delete static_cast<CLogger*>(UserSvr::DllTls(0));
       
    39     UserSvr::DllFreeTls(0);
       
    40 }
       
    41 
       
    42 CLogger& CLogger::InstanceL()
       
    43 {
       
    44     CLogger* self = static_cast<CLogger*>(UserSvr::DllTls(0));
       
    45 
       
    46     if (self == 0)
       
    47     {
       
    48         self = new(ELeave) CLogger;
       
    49         CleanupStack::PushL(self);
       
    50         self->ConstructL();
       
    51         CleanupStack::Pop(self);
       
    52 
       
    53         UserSvr::DllSetTls(0, self);
       
    54     }
       
    55 
       
    56     return *self;
       
    57 }
       
    58 
       
    59 void CLogger::ConstructL()
       
    60 {
       
    61     User::LeaveIfError(iFs.Connect());
       
    62 
       
    63     TFileName logName(PathInfo::PhoneMemoryRootPath());
       
    64     TPtrC appName(BaflUtils::ExtractAppNameFromFullName(RThread().FullName()));
       
    65     logName.Append(appName);
       
    66     _LIT(KLogFileExt, ".log");
       
    67     logName.Append(KLogFileExt);
       
    68 
       
    69     BaflUtils::EnsurePathExistsL(iFs, logName);
       
    70     User::LeaveIfError(iFile.Replace(iFs, logName, EFileShareAny | EFileWrite));
       
    71 }
       
    72 
       
    73 CLogger::~CLogger()
       
    74 {
       
    75     iFile.Close();
       
    76     iFs.Close();
       
    77 }
       
    78 
       
    79 void CLogger::DoWrite(const TDesC8& aText)
       
    80 {
       
    81     const TInt KTimeRecordSize = 12;
       
    82     TBuf8<KTimeRecordSize> timeText;
       
    83 
       
    84     TTime time;
       
    85     time.HomeTime();
       
    86     TDateTime dateTime = time.DateTime();
       
    87 
       
    88     _LIT8(KTimeFormat,"%02d:%02d:%02d.%02d ");
       
    89     timeText.Format(KTimeFormat, dateTime.Hour(), dateTime.Minute(), dateTime.Second(), dateTime.MicroSecond());
       
    90 
       
    91     iFile.Write(timeText);
       
    92     iFile.Write(aText);
       
    93 
       
    94     _LIT8(KNewLine, "\r\n");
       
    95     iFile.Write(KNewLine);
       
    96 //    iFile.Flush();
       
    97 }