| 1 |
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. |
| 2 |
* |
| 3 |
* Redistribution and use in source and binary forms, with or without |
| 4 |
* modification, are permitted provided that the following conditions |
| 5 |
* are met: |
| 6 |
* * Redistributions of source code must retain the above copyright |
| 7 |
* notice, this list of conditions and the following disclaimer. |
| 8 |
* * Redistributions in binary form must reproduce the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer in the |
| 10 |
* documentation and/or other materials provided with the distribution. |
| 11 |
* * Neither the name of NVIDIA CORPORATION nor the names of its |
| 12 |
* contributors may be used to endorse or promote products derived |
| 13 |
* from this software without specific prior written permission. |
| 14 |
* |
| 15 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY |
| 16 |
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 18 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 19 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 20 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 22 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 23 |
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 |
*/ |
| 27 |
|
| 28 |
|
| 29 |
#include <stdio.h> |
| 30 |
#include <string.h> |
| 31 |
#ifdef NVMEDIA_ANDROID |
| 32 |
#define LOG_TAG "nvmedia_common" |
| 33 |
#define LOG_NDEBUG 1 |
| 34 |
#include <utils/Log.h> |
| 35 |
#endif |
| 36 |
#ifdef NVMEDIA_QNX |
| 37 |
#include <sys/slog.h> |
| 38 |
#endif |
| 39 |
|
| 40 |
#include "log_utils.h" |
| 41 |
|
| 42 |
#ifdef NVMEDIA_QNX |
| 43 |
#define NV_SLOGCODE 0xAAAA |
| 44 |
#endif |
| 45 |
#define MAX_STATS_LEN 500 |
| 46 |
|
| 47 |
#define LOG_BUFFER_BYTES 1024 |
| 48 |
|
| 49 |
static enum LogLevel msg_level = LEVEL_ERR; |
| 50 |
static enum LogStyle msg_style = LOG_STYLE_NORMAL; |
| 51 |
static FILE *msg_file = NULL; |
| 52 |
|
| 53 |
void SetLogLevel(enum LogLevel level) |
| 54 |
{ |
| 55 |
if (level > LEVEL_DBG) |
| 56 |
return; |
| 57 |
|
| 58 |
msg_level = level; |
| 59 |
} |
| 60 |
|
| 61 |
void SetLogStyle(enum LogStyle style) |
| 62 |
{ |
| 63 |
if (style > LOG_STYLE_FUNCTION_LINE) |
| 64 |
return; |
| 65 |
|
| 66 |
msg_style = style; |
| 67 |
} |
| 68 |
|
| 69 |
void SetLogFile(FILE *logFileHandle) |
| 70 |
{ |
| 71 |
if(!logFileHandle) |
| 72 |
return; |
| 73 |
|
| 74 |
msg_file = logFileHandle; |
| 75 |
} |
| 76 |
|
| 77 |
void LogLevelMessage(enum LogLevel level, const char *functionName, |
| 78 |
int lineNumber, const char *format, ...) |
| 79 |
{ |
| 80 |
va_list ap; |
| 81 |
char str[LOG_BUFFER_BYTES] = {'\0',}; |
| 82 |
FILE *logFile = msg_file ? msg_file : stdout; |
| 83 |
|
| 84 |
if (level > msg_level) |
| 85 |
return; |
| 86 |
|
| 87 |
#ifndef NVMEDIA_ANDROID |
| 88 |
/** In the case of Android ADB log, if LOG_TAG is defined, |
| 89 |
* before 'Log.h' is included in source file, |
| 90 |
* LOG_TAG is automatically concatenated at the beginning of log message, |
| 91 |
* so, we don't copy 'nvmedia: ' into 'str'. |
| 92 |
*/ |
| 93 |
strcpy(str, "nvmedia: "); |
| 94 |
|
| 95 |
/** As LOG_TAG is concatednated, log level is also automatically concatenated, |
| 96 |
* by calling different ADB log function such as ALOGE(for eror log message), |
| 97 |
* ALOGW(for warning log message). |
| 98 |
*/ |
| 99 |
switch (level) { |
| 100 |
case LEVEL_ERR: |
| 101 |
strcat(str, "ERROR: "); |
| 102 |
break; |
| 103 |
case LEVEL_WARN: |
| 104 |
strcat(str, "WARNING: "); |
| 105 |
break; |
| 106 |
case LEVEL_INFO: |
| 107 |
case LEVEL_DBG: |
| 108 |
// Empty |
| 109 |
break; |
| 110 |
} |
| 111 |
#endif |
| 112 |
|
| 113 |
va_start(ap, format); |
| 114 |
vsnprintf(str + strlen(str), sizeof(str) - strlen(str), format, ap); |
| 115 |
|
| 116 |
if(msg_style == LOG_STYLE_NORMAL) { |
| 117 |
// Add trailing new line char |
| 118 |
if(strlen(str) && str[strlen(str) - 1] != '\n') |
| 119 |
strcat(str, "\n"); |
| 120 |
|
| 121 |
} else if(msg_style == LOG_STYLE_FUNCTION_LINE) { |
| 122 |
// Remove trailing new line char |
| 123 |
if(strlen(str) && str[strlen(str) - 1] == '\n') |
| 124 |
str[strlen(str) - 1] = 0; |
| 125 |
|
| 126 |
// Add function and line info |
| 127 |
snprintf(str + + strlen(str), sizeof(str) - strlen(str), " at %s():%d\n", functionName, lineNumber); |
| 128 |
} |
| 129 |
|
| 130 |
#ifdef NVMEDIA_ANDROID |
| 131 |
switch (msg_level) { |
| 132 |
case LEVEL_ERR: |
| 133 |
ALOGE("%s", str); |
| 134 |
break; |
| 135 |
case LEVEL_WARN: |
| 136 |
ALOGW("%s", str); |
| 137 |
break; |
| 138 |
case LEVEL_INFO: |
| 139 |
ALOGI("%s", str); |
| 140 |
break; |
| 141 |
case LEVEL_DBG: |
| 142 |
ALOGD("%s", str); |
| 143 |
break; |
| 144 |
} |
| 145 |
#else |
| 146 |
fprintf(logFile, "%s", str); |
| 147 |
#endif |
| 148 |
#ifdef NVMEDIA_QNX |
| 149 |
/* send to system logger */ |
| 150 |
slogf(_SLOG_SETCODE(NV_SLOGCODE, 0), _SLOG_ERROR, str); |
| 151 |
#endif |
| 152 |
va_end(ap); |
| 153 |
} |
| 154 |
|