Commit 3c012d4e authored by Giovanne Marcelo's avatar Giovanne Marcelo

Merge branch 'log' into 'master'

Log



See merge request !2
parents 72148f89 2dee8bae
Pipeline #6326 failed with stage
in 1 minute and 40 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)
......
......@@ -10,6 +10,7 @@
#include <string.h>
#include <jansson.h>
#include <stdlib.h>
#include "log.h"
typedef PGresult DBresult;
......
#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
......@@ -21,8 +21,7 @@ void read_conf(const char *path, char * conninfo) {
json_t *config = json_load_file(path,JSON_ENCODE_ANY,&error);
if(!config) {
fprintf(stderr, "Error in database config file.\n");
fprintf(stderr, "%s\n", error.text);
error(error.text);
exit(-1);
}
......@@ -46,12 +45,11 @@ PGconn* db_connect() {
// Validate
if(!conn) {
fprintf(stderr, "Error in create a connection with db.\n" );
error("Error in create a connection with db.\n" );
exit(-1);
}
else if( PQstatus(conn) == CONNECTION_BAD ) {
fprintf(stderr, "Conection to database failed.\n");
fprintf(stderr, "%s", PQerrorMessage(conn));
error(PQerrorMessage(conn));
exit(-1);
}
return conn;
......@@ -74,11 +72,10 @@ DBresult* db_select( char *columns, char *table, char *where) {
// Validate
if(!result){
fprintf(stderr, "Error in executing the command.\n");
error("Error in executing the command.");
}
else if ( PQresultStatus(result) == PGRES_FATAL_ERROR ) {
fprintf(stderr, "Error in query.\n");
fprintf(stderr, "%s\n", PQresultErrorMessage(result));
error(PQresultErrorMessage(result));
exit(-1);
}
db_disconnect(conn);
......
/*
* 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);
fprintf(stderr, "[ERROR] Check log for more details.\n");
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);
fprintf(stderr, "[WARN] Check log for more details.\n");
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