os/graphics/m3g/m3gcore11/src/m3g_symbian.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     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 the License "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: Symbian debugging and profiling functions
    15 *
    16 */
    17 
    18 #include "m3g_defs.h"
    19 #include <e32base.h>
    20 #include <f32file.h>
    21 #include <ezlib.h>
    22 
    23 #if defined(M3G_SYMBIAN_REMOTE_LOGGING)
    24 #include <EcmtClient.h>
    25 #endif
    26 
    27 struct M3GLogger
    28 {
    29     M3Gint refCount;
    30     
    31 #   if defined(M3G_SYMBIAN_REMOTE_LOGGING)
    32     REcmt iEcmt;
    33 #   endif
    34 
    35 #   if defined(M3G_ENABLE_PROFILING)
    36     M3Guint tickCount[M3G_STAT_MAX];
    37     M3Guint lastOutputTickCount;
    38 #   endif
    39 };
    40 
    41 /*----------------------------------------------------------------------
    42  * Internal functions
    43  *--------------------------------------------------------------------*/
    44 
    45 /*!
    46  * \brief Symbian implementation of the block inflation function for
    47  * the Loader class
    48  */
    49 extern "C" M3Gsizei m3gSymbianInflateBlock(M3Gsizei srcLength,
    50                                            const M3Gubyte *src,
    51                                            M3Gsizei dstLength,
    52                                            M3Gubyte *dst)
    53 {
    54     M3G_ASSERT(src);
    55     M3G_ASSERT(dst);
    56     M3G_ASSERT(srcLength > 0);
    57 
    58     {
    59         uLongf len = (uLongf) dstLength;
    60         if (uncompress((Bytef *) dst, &len,
    61                        (const Bytef *) src, (uLong) srcLength) != Z_OK) {
    62             return 0;
    63         }
    64         return (M3Gsizei) len;
    65     }
    66 }
    67 
    68 /*!
    69  * \brief Logging function
    70  *
    71  * \note This currently creates a session to the file server for each
    72  * message, so performance will be sub-optimal if logging is frequent
    73  */
    74 extern "C" void m3gLogMessage(const char *format, ...)
    75 {    
    76 #   if !defined(M3G_SYMBIAN_REMOTE_LOGGING)
    77     _LIT(KFileName, "c:\\m3g_event.log");
    78 
    79     RFs fileSrv;
    80     RFile file;
    81 
    82     if (fileSrv.Connect() != KErrNone) {
    83         return;
    84     }
    85 
    86     TInt accessMode = EFileWrite|EFileShareExclusive;
    87     TInt err = file.Open(fileSrv, KFileName, accessMode);
    88 
    89     // If the file exists, seek to the end; if it doesn't, create it
    90     
    91     if (err == KErrNone) {
    92         TInt pos = 0;
    93         file.Seek(ESeekEnd, pos);
    94     }
    95     else if (err == KErrNotFound) {
    96         if (file.Create(fileSrv, KFileName, accessMode) != KErrNone) {
    97             fileSrv.Close();
    98             return;
    99         }
   100     }
   101 #   endif // !M3G_SYMBIAN_REMOTE_LOGGING
   102 
   103     // Format and write the message
   104 
   105     TBuf8<1024> msg;
   106     TPtrC8 aFormat((const TUint8 *) format);
   107     
   108     VA_LIST aList;
   109     VA_START(aList, format);
   110 
   111     msg.FormatList(aFormat, aList);
   112 
   113     VA_END(aList);
   114 
   115 #   if defined(M3G_SYMBIAN_REMOTE_LOGGING)
   116 
   117     M3GLogger *logger = (M3GLogger*) Dll::Tls();
   118 
   119     if (logger) {
   120         TBuf16<1024> msg16;
   121         msg16.Copy(msg);
   122         logger->iEcmt.Write(msg16);
   123     }
   124     
   125 #   else
   126     file.Write(msg);
   127 
   128     // Close the file and the server connection
   129     
   130     file.Flush();
   131     file.Close();
   132     fileSrv.Close();
   133     
   134 #   endif // !M3G_SYMBIAN_REMOTE_LOGGING
   135 }
   136 
   137 /*!
   138  * \brief Create new log file
   139  */
   140 extern "C" void m3gBeginLog(void)
   141 {
   142 #   if defined(M3G_SYMBIAN_REMOTE_LOGGING)
   143 
   144     M3GLogger *logger = (M3GLogger*) Dll::Tls();
   145     
   146     if (!logger) {
   147         logger = new M3GLogger();
   148         logger->refCount = 0;
   149         logger->lastOutputTickCount = User::TickCount();
   150         Dll::SetTls(logger);
   151     }
   152 
   153     if (++logger->refCount == 1) {
   154         logger->iEcmt.Connect();
   155     }
   156 
   157 #   else
   158     
   159     /* Just delete any existing log file */
   160     
   161     _LIT(KFileName, "c:\\m3g_event.log");
   162 
   163     RFs fileSrv;
   164     RFile file;
   165 
   166     if (fileSrv.Connect() != KErrNone) {
   167         return;
   168     }
   169 
   170     TInt accessMode = EFileWrite|EFileShareExclusive;
   171     if (file.Replace(fileSrv, KFileName, accessMode) == KErrNone) {
   172         file.Flush();
   173         file.Close();
   174     }
   175     fileSrv.Close();
   176     
   177 #   endif // !M3G_SYMBIAN_REMOTE_LOGGING
   178     
   179     /* Output initial message(s) */
   180     
   181     m3gLogMessage("--- M3G event log ---\n");
   182 }
   183 
   184 /*!
   185  * \brief End logging
   186  */
   187 extern "C" void m3gEndLog(void)
   188 {
   189 #   if defined(M3G_SYMBIAN_REMOTE_LOGGING)
   190 
   191     M3GLogger *logger = (M3GLogger*) Dll::Tls();
   192     if (logger) {
   193         if (--logger->refCount == 0) {
   194             logger->iEcmt.Close();
   195             Dll::FreeTls();
   196             delete logger;
   197         }
   198     }
   199 
   200 #   endif
   201     
   202     m3gLogMessage("--- end of log ---\n");
   203 }
   204 
   205 /*!
   206  * \brief Assertion handler
   207  */
   208 extern "C" void m3gAssertFailed(const char *filename, int line)
   209 {
   210     M3G_LOG2(M3G_LOG_ALL, "Assertion failed: %s, line %d\n", filename, line);
   211     User::Panic(_L("M3G-ASSERT"), line);
   212 }
   213 
   214 #if defined(M3G_BUILD_DLL)
   215 /*!
   216  * \brief DLL load check
   217  */
   218 #ifndef EKA2
   219 GLDEF_C TInt E32Dll( TDllReason /* aReason */ )
   220 {
   221     return KErrNone;
   222 }
   223 #endif
   224 #endif /* M3G_BUILD_DLL */
   225 
   226 /*----------------------------------------------------------------------
   227  * Profiling
   228  *--------------------------------------------------------------------*/
   229 
   230 #if defined(M3G_ENABLE_PROFILING)
   231 
   232 extern "C" void m3gCleanupProfile(void)
   233 {
   234     ;
   235 }
   236 
   237 extern "C" void m3gBeginProfile(int stat)
   238 {
   239     M3GLogger *logger = (M3GLogger*) Dll::Tls();
   240     if (logger) {
   241         logger->tickCount[stat] = User::TickCount();
   242     }
   243 }
   244 
   245 extern "C" int m3gEndProfile(int stat)
   246 {
   247     M3GLogger *logger = (M3GLogger*) Dll::Tls();
   248     if (logger) {
   249         return User::TickCount() - logger->tickCount[stat];
   250     }
   251     return 0;
   252 }
   253 
   254 extern "C" M3Gint m3gProfileTriggered(void)
   255 {
   256 #   if (M3G_PROFILE_LOG_INTERVAL > 0)
   257     M3GLogger *logger = (M3GLogger*) Dll::Tls();
   258     if (logger) {
   259         M3Guint tickCount = User::TickCount();
   260         M3Guint delta = tickCount - logger->lastOutputTickCount;
   261         if (delta >= M3G_PROFILE_LOG_INTERVAL) {
   262             logger->lastOutputTickCount = tickCount;
   263             return (M3Gint) delta;
   264         }
   265     }
   266 #   endif
   267     return 0;
   268 }
   269 
   270 #endif
   271