Skip to content

Commit

Permalink
move includes and constants to header, use SymDate
Browse files Browse the repository at this point in the history
  • Loading branch information
erilong committed Oct 12, 2015
1 parent 6b78d52 commit ea85374
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 32 deletions.
22 changes: 20 additions & 2 deletions symmetric-client-clib/inc/common/Log.h
Expand Up @@ -19,11 +19,29 @@
* under the License.
*/

typedef enum {DEBUG, INFO, WARN, ERROR} LogLevel;
#ifndef SYM_LOG_H
#define SYM_LOG_H

#include <stdio.h>
#include <stdarg.h>
#include "common/Log.h"
#include "util/StringBuilder.h"
#include "util/Date.h"
#include <time.h>

typedef enum {DEBUG, INFO, WARN, ERROR} SymLogLevel;

#define SYM_LOG_LEVEL_DEBUG "DEBUG"
#define SYM_LOG_LEVEL_INFO "INFO"
#define SYM_LOG_LEVEL_WARN "WARN"
#define SYM_LOG_LEVEL_ERROR "ERROR"
#define SYM_LOG_LEVEL_UNKNOWN "UNKNOWN"

#define SymLog_debug(M, ...) SymLog_log(0, __func__, __FILE__, __LINE__, M, ##__VA_ARGS__)
#define SymLog_info(M, ...) SymLog_log(1, __func__, __FILE__, __LINE__, M, ##__VA_ARGS__)
#define SymLog_warn(M, ...) SymLog_log(2, __func__, __FILE__, __LINE__, M, ##__VA_ARGS__)
#define SymLog_error(M, ...) SymLog_log(3, __func__, __FILE__, __LINE__, M, ##__VA_ARGS__)

void SymLog_log(LogLevel logLevel, const char *functionName, const char *filename, int lineNumber, const char* message, ...);
void SymLog_log(SymLogLevel logLevel, const char *functionName, const char *filename, int lineNumber, const char* message, ...);

#endif
40 changes: 10 additions & 30 deletions symmetric-client-clib/src/common/SymLog.c
Expand Up @@ -18,44 +18,25 @@
* specific language governing permissions and limitations
* under the License.
*/

#include <stdio.h>
#include <stdarg.h>
#include "common/Log.h"
#include "util/StringBuilder.h"
#include <time.h>

char* logLevelDescription(LogLevel logLevel) {
static char* logLevelDescription(SymLogLevel logLevel) {
switch (logLevel) {
case DEBUG:
return "DEBUG";
return SYM_LOG_LEVEL_DEBUG;
case INFO:
return "INFO";
return SYM_LOG_LEVEL_INFO;
case WARN:
return "WARN";
return SYM_LOG_LEVEL_WARN;
case ERROR:
return "ERROR";
return SYM_LOG_LEVEL_ERROR;
default:
return "<UNKNOWN>";
return SYM_LOG_LEVEL_UNKNOWN;
}
}

char *generateTimestamp() {
int TIMESTAMP_LENGTH = 26;
time_t timer;
char* buffer = malloc(TIMESTAMP_LENGTH);
// char buffer[26];
struct tm* tm_info;

time(&timer);
tm_info = localtime(&timer);

strftime(buffer, TIMESTAMP_LENGTH, "%Y-%m-%dT%H:%M:%S", tm_info);
return buffer;
}

/** This is the central place where all logging funnels through. */
void SymLog_log(LogLevel logLevel, const char *functionName, const char *fileName, int lineNumber, const char* message, ...) {
void SymLog_log(SymLogLevel logLevel, const char *functionName, const char *fileName, int lineNumber, const char* message, ...) {
FILE *destination;
if (logLevel <= INFO) {
destination = stdout;
Expand All @@ -68,9 +49,9 @@ void SymLog_log(LogLevel logLevel, const char *functionName, const char *fileNam

SymStringBuilder *messageBuffer = SymStringBuilder_new();

char* logTimestamp = generateTimestamp();
SymDate *date = SymDate_new();

messageBuffer->append(messageBuffer, logTimestamp);
messageBuffer->append(messageBuffer, date->dateTimeString);
messageBuffer->append(messageBuffer, " [");
messageBuffer->append(messageBuffer, levelDescription);
messageBuffer->append(messageBuffer, "] [");
Expand All @@ -84,7 +65,6 @@ void SymLog_log(LogLevel logLevel, const char *functionName, const char *fileNam
va_start(varargs, message);
messageBuffer->appendfv(messageBuffer, message, varargs);
va_end(varargs);

messageBuffer->append(messageBuffer, "\n");

fprintf(destination, "%s", messageBuffer->toString(messageBuffer));
Expand All @@ -93,7 +73,7 @@ void SymLog_log(LogLevel logLevel, const char *functionName, const char *fileNam
// Do this to keep log messages more or less in order.
fflush(destination);

date->destroy(date);
messageBuffer->destroy(messageBuffer);
free(logTimestamp);
}

0 comments on commit ea85374

Please sign in to comment.