Commit 2dee8bae authored by Giovanne Marcelo's avatar Giovanne Marcelo

add log in db api

parent 630b3f7a
Pipeline #6325 failed with stage
in 1 minute and 30 seconds
......@@ -10,6 +10,7 @@
#include <string.h>
#include <jansson.h>
#include <stdlib.h>
#include "log.h"
typedef PGresult DBresult;
......
......@@ -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);
......
......@@ -21,6 +21,7 @@ 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);
}
......@@ -28,6 +29,7 @@ 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);
}
......
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