AppDynamics IoT C++ SDK
AppDynamics IoT C++ library contains code that facilitates capturing availability, usage, network performance and errors of an IoT Application.
log.cpp File Reference
#include <unistd.h>
#include <sys/uio.h>
#include <string.h>
#include <fcntl.h>
#include <inttypes.h>
#include "log.hpp"
#include "config.hpp"

Macros

#define __STDC_FORMAT_MACROS
 
#define LOG_MAX_SIZE   2048
 
#define LOG_HEADER   "E/APPDYNAMICS: "
 
#define LOG_HEADER_LEN   (sizeof(LOG_HEADER) - 1)
 
#define ERROR_LOG_MSG   "<NULL Log Message>"
 

Functions

static void log_write_to_stderr (const char *logmsg, size_t logmsg_len)
 Prefixes Log Message with timestamp and writes to stderr.
More...
 
void appd_iot_log (appd_iot_log_level_t log_level, const char *format,...)
 Reads log message, appends log header and triggers log write callback function. More...
 
const char * appd_iot_error_code_to_str (appd_iot_error_code_t error_code)
 Convert error code to string. More...
 
const char * appd_iot_sdk_state_to_str (appd_iot_sdk_state_t sdk_state)
 Convert sdk state to string. More...
 

Variables

static char loglevel_c [APPD_IOT_MAX_LOG_LEVELS] = {'O', 'E', 'W', 'I', 'D', 'V', 'A'}
 
static const char * error_code_to_str [APPD_IOT_MAX_ERROR_CODES]
 
static const char * sdk_state_to_str [APPD_IOT_MAX_SDK_STATES]
 

Macro Definition Documentation

◆ __STDC_FORMAT_MACROS

#define __STDC_FORMAT_MACROS

◆ LOG_MAX_SIZE

#define LOG_MAX_SIZE   2048

◆ LOG_HEADER

#define LOG_HEADER   "E/APPDYNAMICS: "

◆ LOG_HEADER_LEN

#define LOG_HEADER_LEN   (sizeof(LOG_HEADER) - 1)

◆ ERROR_LOG_MSG

#define ERROR_LOG_MSG   "<NULL Log Message>"

Function Documentation

◆ log_write_to_stderr()

static void log_write_to_stderr ( const char *  logmsg,
size_t  logmsg_len 
)
static

Prefixes Log Message with timestamp and writes to stderr.

Parameters
logmsgcontains the log message without the newline char at the end
logmsg_lencontains the length of log message
151 {
152  if (logmsg == NULL)
153  {
154  return;
155  }
156 
157  int iovcnt;
158  struct iovec iov[3];
159  char timestamp[32];
160  char eol = '\n';
161 
162  struct timeval tv;
163  gettimeofday(&tv, NULL);
164 
165  int64_t time_in_ms = (int64_t)tv.tv_sec * 1000 + (int64_t)tv.tv_usec / 1000;
166  size_t timestamp_len = snprintf(timestamp, sizeof(timestamp), "%" PRId64 " ", time_in_ms);
167 
168  if (timestamp_len >= sizeof(timestamp))
169  {
170  timestamp_len = sizeof(timestamp) - 1;
171  timestamp[timestamp_len] = '\0';
172  }
173 
174  iov[0].iov_base = (void*)timestamp;
175  iov[0].iov_len = timestamp_len;
176 
177  iov[1].iov_base = (void*)logmsg;
178  iov[1].iov_len = logmsg_len;
179 
180  iov[2].iov_base = (void*)(&eol);
181  iov[2].iov_len = sizeof(eol);
182 
183  iovcnt = sizeof(iov) / sizeof(struct iovec);
184 
185  writev(STDERR_FILENO, iov, iovcnt);
186 }

◆ appd_iot_log()

void appd_iot_log ( appd_iot_log_level_t  log_level,
const char *  format,
  ... 
)

Reads log message, appends log header and triggers log write callback function.

Parameters
log_levelindicates log level listed in log_detail_t
formatPrintf Format String
90 {
91  //check for log level
92  appd_iot_log_level_t configlog_level = appd_iot_get_log_level();
93 
94  if (log_level > configlog_level)
95  {
96  return;
97  }
98 
99  if (log_level >= APPD_IOT_MAX_LOG_LEVELS)
100  {
101  log_level = APPD_IOT_LOG_ERROR;
102  }
103 
104  //check for log write cb
106 
107  if (log_write_cb == NULL)
108  {
109  log_write_cb = &log_write_to_stderr;
110  }
111 
112  //initilize logbuf with log header
113  char logbuf[LOG_MAX_SIZE] = LOG_HEADER;
114  size_t logmsg_len;
115 
116  //update log level in the log header
117  logbuf[0] = loglevel_c[log_level];
118 
119  va_list args;
120 
121  //read log message
122  va_start(args, format);
123  int nchar = vsnprintf(logbuf + LOG_HEADER_LEN, (LOG_MAX_SIZE - LOG_HEADER_LEN), format, args);
124  va_end(args);
125 
126  if (nchar < 0)
127  {
128  snprintf(logbuf + LOG_HEADER_LEN, sizeof(ERROR_LOG_MSG), ERROR_LOG_MSG);
129  logmsg_len = LOG_HEADER_LEN + sizeof(ERROR_LOG_MSG) - 1;
130  }
131  else if (LOG_HEADER_LEN + nchar >= LOG_MAX_SIZE)
132  {
133  logmsg_len = LOG_MAX_SIZE - 1;
134  logbuf[logmsg_len] = '\0';
135  }
136  else
137  {
138  logmsg_len = LOG_HEADER_LEN + nchar;
139  }
140 
141  //trigger callback to write log msg
142  log_write_cb(logbuf, logmsg_len);
143 }
#define ERROR_LOG_MSG
Definition: log.cpp:37
Definition: appd_iot_def.h:111
appd_iot_log_write_cb_t appd_iot_get_log_write_cb(void)
Get Log Write Callback Function Pointer.
Definition: config.cpp:184
appd_iot_log_level_t
Log Level Enums returned for all API calls.
Definition: appd_iot_def.h:94
static char loglevel_c[APPD_IOT_MAX_LOG_LEVELS]
Definition: log.cpp:39
#define LOG_HEADER_LEN
Definition: log.cpp:35
void(* appd_iot_log_write_cb_t)(const char *logmsg, size_t logmsg_len)
Log Write Callback implements the functionality to process log messages The callback implementation...
Definition: appd_iot_def.h:124
#define LOG_HEADER
Definition: log.cpp:34
#define LOG_MAX_SIZE
Definition: log.cpp:31
Definition: appd_iot_def.h:99
appd_iot_log_level_t appd_iot_get_log_level(void)
Get Log Level configured as part of SDK Initialization.
Definition: config.cpp:174
static void log_write_to_stderr(const char *logmsg, size_t logmsg_len)
Prefixes Log Message with timestamp and writes to stderr.
Definition: log.cpp:150

◆ appd_iot_error_code_to_str()

const char* appd_iot_error_code_to_str ( appd_iot_error_code_t  error_code)

Convert error code to string.

Parameters
error_codethat is to be converted to string
Returns
string representation of the error code
194 {
195  return error_code_to_str[error_code];
196 }
static const char * error_code_to_str[APPD_IOT_MAX_ERROR_CODES]
Definition: log.cpp:43

◆ appd_iot_sdk_state_to_str()

const char* appd_iot_sdk_state_to_str ( appd_iot_sdk_state_t  sdk_state)

Convert sdk state to string.

Parameters
sdk_statethat is to be converted to string
Returns
string representation of the sdk state
204 {
205  return sdk_state_to_str[sdk_state];
206 }
static const char * sdk_state_to_str[APPD_IOT_MAX_SDK_STATES]
Definition: log.cpp:70

Variable Documentation

◆ loglevel_c

char loglevel_c[APPD_IOT_MAX_LOG_LEVELS] = {'O', 'E', 'W', 'I', 'D', 'V', 'A'}
static

◆ error_code_to_str

const char* error_code_to_str[APPD_IOT_MAX_ERROR_CODES]
static
Initial value:
=
{
"SUCCESS",
"INVALID_INPUT",
"MAX_LIMIT",
"NETWORK_ERROR",
"NETWORK_REJECT",
"NETWORK_UNREACHABLE",
"NETWORK_NOT_AVAILABLE",
"NULL_PTR",
"INTERNAL_ERROR",
"NOT_SUPPORTED",
"SDK_NOT_ENABLED",
}

◆ sdk_state_to_str

const char* sdk_state_to_str[APPD_IOT_MAX_SDK_STATES]
static
Initial value:
=
{
"SDK_UNINITIALIZED",
"SDK_ENABLED",
"SDK_DISABLED_KILL_SWITCH",
"SDK_DISABLED_LICENSE_EXPIRED",
"SDK_DISABLED_DATA_LIMIT_EXCEEDED"
}