972a83f4e68fa5f7549188072295278ec4db69fb
1 /**
2 * Copyright (C) ARM Limited 2010-2011. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
9 typedef unsigned long long uint64_t;
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <stdarg.h>
13 #include <string.h>
14 #include "OlyUtility.h"
16 #ifdef WIN32
17 #define MUTEX_INIT() mLoggingMutex = CreateMutex(NULL, false, NULL);
18 #define MUTEX_LOCK() WaitForSingleObject(mLoggingMutex, 0xFFFFFFFF);
19 #define MUTEX_UNLOCK() ReleaseMutex(mLoggingMutex);
20 #define snprintf _snprintf
21 #else
22 #include <pthread.h>
23 #define MUTEX_INIT() pthread_mutex_init(&mLoggingMutex, NULL)
24 #define MUTEX_LOCK() pthread_mutex_lock(&mLoggingMutex)
25 #define MUTEX_UNLOCK() pthread_mutex_unlock(&mLoggingMutex)
26 #endif
28 #include "Logging.h"
30 // Global thread-safe logging
31 Logging* logg = NULL;
33 Logging::Logging(bool debug) {
34 mFileCreated = false;
35 mWarningXMLPath[0] = 0;
36 mDebug = debug;
37 MUTEX_INIT();
39 strcpy(mErrBuf, "Unknown Error");
40 strcpy(mLogBuf, "Unknown Message");
41 }
43 Logging::~Logging() {
44 if (mFileCreated) {
45 util->appendToDisk(mWarningXMLPath, "</warnings>");
46 }
47 }
49 void Logging::logError(const char* file, int line, const char* fmt, ...) {
50 va_list args;
52 MUTEX_LOCK();
53 if (mDebug) {
54 snprintf(mErrBuf, sizeof(mErrBuf), "ERROR[%s:%d]: ", file, line);
55 } else {
56 mErrBuf[0] = 0;
57 }
59 va_start(args, fmt);
60 vsnprintf(mErrBuf + strlen(mErrBuf), sizeof(mErrBuf) - 2 - strlen(mErrBuf), fmt, args); // subtract 2 for \n and \0
61 va_end(args);
63 // Add the message to the warning file if the warning file was created
64 if (mWarningXMLPath[0] != 0) {
65 if (!mFileCreated) {
66 if (util->writeToDisk(mWarningXMLPath, "<?xml version=\"1.0\" encoding='UTF-8'?>\n<warnings version=\"1\">\n") < 0) {
67 return;
68 }
69 mFileCreated = true;
70 }
71 util->appendToDisk(mWarningXMLPath, " <warning text=\"");
72 util->appendToDisk(mWarningXMLPath, mErrBuf);
73 util->appendToDisk(mWarningXMLPath, "\"/>\n");
74 }
75 if (strlen(mErrBuf) > 0) {
76 strcat(mErrBuf, "\n");
77 }
78 MUTEX_UNLOCK();
79 }
81 void Logging::logMessage(const char* fmt, ...) {
82 if (mDebug) {
83 va_list args;
85 MUTEX_LOCK();
86 strcpy(mLogBuf, "INFO: ");
88 va_start(args, fmt);
89 vsnprintf(mLogBuf + strlen(mLogBuf), sizeof(mLogBuf) - 2 - strlen(mLogBuf), fmt, args); // subtract 2 for \n and \0
90 va_end(args);
91 strcat(mLogBuf, "\n");
93 fprintf(stdout, "%s", mLogBuf);
94 fflush(stdout);
95 MUTEX_UNLOCK();
96 }
97 }