Commit 630b3f7a authored by Giovanne Marcelo's avatar Giovanne Marcelo

add log module

parent 17751df2
Pipeline #6322 failed with stage
in 1 minute and 34 seconds
......@@ -3,8 +3,8 @@ CFLAGS = -Wall -Wextra -Wconversion -fstack-protector -Wformat-security -g -O3
LFLAGS = -lpq -ljansson
INCL = -I./include
SRC = ./src
DEPS = ./include/db_api.h
OBJ = db_api.o
DEPS = ./include/db_api.h ./include/log.h
OBJ = db_api.o log.o
APP=recommender
$(APP): $(OBJ)
......
#ifndef _LOG_H
#define _LOG_H
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <sys/stat.h>
#include <string.h>
#define error(msg) log_error(msg, __LINE__, __FILE__)
#define info(msg) log_info(msg, __LINE__, __FILE__)
#define warn(msg) log_warn(msg, __LINE__, __FILE__)
FILE* open_log();
void log_error(char* msg, int line, char *file);
void log_warn(char* msg, int line, char *file);
void log_info(char* msg, int line, char *file);
void close_log(FILE *stream);
void write_date(FILE *stream);
#endif
/*
* Implementation of functions that display messages in log file
*
* This source code is licensed under the GNU General Public License,
* Version 3. See the file LICENSE for more details.
*
*/
#include "log.h"
FILE* open_log() {
FILE * stream;
struct stat st = {0};
if( stat("./log", &st) == -1 ) {
mkdir("./log", 0755);
}
stream = fopen("./log/recommender.log","a+");
return stream;
}
void log_error(char* msg, int line, char *file) {
FILE *stream = open_log();
write_date(stream);
fprintf(stream, "%s:%d - [ERROR] - %s\n",file, line, msg);
close_log(stream);
}
void log_warn(char* msg, int line, char *file) {
FILE *stream = open_log();
write_date(stream);
fprintf(stream, "%s:%d - [WARN] - %s\n", file, line, msg);
close_log(stream);
}
void log_info(char* msg, int line, char *file) {
FILE *stream = open_log();
write_date(stream);
fprintf(stream, "%s:%d - [INFO] - %s\n", file, line, msg);
close_log(stream);
}
void close_log(FILE *stream) {
fclose(stream);
}
void write_date(FILE *stream){
time_t current_time = time(NULL);
char *date = asctime(gmtime(&current_time));
date[strlen(date) - 1] = 0;
fprintf(stream, "%s - ", date);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment