mirror of
https://github.com/eddyem/pusirobot.git
synced 2025-12-06 10:35:11 +03:00
Take logging from new usefull_macros library
This commit is contained in:
parent
0a312fd010
commit
18d489d84f
@ -103,87 +103,6 @@ char *find_device(){
|
||||
return path;
|
||||
}
|
||||
|
||||
Cl_log *globlog = NULL;
|
||||
|
||||
/**
|
||||
* @brief Cl_createlog - create log file: init mutex, test file open ability
|
||||
* @param logpath - path to log file
|
||||
* @param level - lowest message level (e.g. LOGLEVEL_ERR won't allow to write warn/msg/dbg)
|
||||
* @return allocated structure (should be free'd later by Cl_deletelog) or NULL
|
||||
*/
|
||||
Cl_log *Cl_createlog(const char *logpath, Cl_loglevel level){
|
||||
if(level < LOGLEVEL_NONE || level > LOGLEVEL_ANY) return NULL;
|
||||
if(!logpath) return NULL;
|
||||
FILE *logfd = fopen(logpath, "a");
|
||||
if(!logfd){
|
||||
WARN("Can't open log file");
|
||||
return NULL;
|
||||
}
|
||||
fclose(logfd);
|
||||
Cl_log *log = MALLOC(Cl_log, 1);
|
||||
if(pthread_mutex_init(&log->mutex, NULL)){
|
||||
WARN("Can't init log mutex");
|
||||
FREE(log);
|
||||
return NULL;
|
||||
}
|
||||
log->logpath = strdup(logpath);
|
||||
if(!log->logpath){
|
||||
WARN("strdup()");
|
||||
FREE(log);
|
||||
return NULL;
|
||||
}
|
||||
log->loglevel = level;
|
||||
return log;
|
||||
}
|
||||
|
||||
void Cl_deletelog(Cl_log **log){
|
||||
if(!log || !*log) return;
|
||||
FREE((*log)->logpath);
|
||||
FREE(*log);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Cl_putlog - put message to log file with/without timestamp
|
||||
* @param timest - ==1 to put timestamp
|
||||
* @param log - pointer to log structure
|
||||
* @param lvl - message loglevel (if lvl > loglevel, message won't be printed)
|
||||
* @param fmt - format and the rest part of message
|
||||
* @return amount of symbols saved in file
|
||||
*/
|
||||
int Cl_putlogt(int timest, Cl_log *log, Cl_loglevel lvl, const char *fmt, ...){
|
||||
if(!log || !log->logpath) return 0;
|
||||
if(lvl > log->loglevel) return 0;
|
||||
if(pthread_mutex_lock(&log->mutex)){
|
||||
WARN("Can't lock log mutex");
|
||||
return 0;
|
||||
}
|
||||
int i = 0;
|
||||
FILE *logfd = fopen(log->logpath, "a+");
|
||||
if(!logfd) goto rtn;
|
||||
if(timest){
|
||||
char strtm[128];
|
||||
time_t t = time(NULL);
|
||||
struct tm *curtm = localtime(&t);
|
||||
strftime(strtm, 128, "%Y/%m/%d-%H:%M:%S", curtm);
|
||||
i = fprintf(logfd, "%s", strtm);
|
||||
}
|
||||
i += fprintf(logfd, "\t");
|
||||
va_list ar;
|
||||
va_start(ar, fmt);
|
||||
i += vfprintf(logfd, fmt, ar);
|
||||
va_end(ar);
|
||||
fseek(logfd, -1, SEEK_CUR);
|
||||
char c;
|
||||
ssize_t r = fread(&c, 1, 1, logfd);
|
||||
if(1 == r){ // add '\n' if there was no newline
|
||||
if(c != '\n') i += fprintf(logfd, "\n");
|
||||
}
|
||||
fclose(logfd);
|
||||
rtn:
|
||||
pthread_mutex_unlock(&log->mutex);
|
||||
return i;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief str2long - convert full string to long
|
||||
* @param str - string
|
||||
|
||||
@ -22,37 +22,6 @@
|
||||
|
||||
#include "cmdlnopts.h"
|
||||
|
||||
typedef enum{
|
||||
LOGLEVEL_NONE, // no logs
|
||||
LOGLEVEL_ERR, // only errors
|
||||
LOGLEVEL_WARN, // only warnings and errors
|
||||
LOGLEVEL_MSG, // all without debug
|
||||
LOGLEVEL_DBG, // all messages
|
||||
LOGLEVEL_ANY // all shit
|
||||
} Cl_loglevel;
|
||||
|
||||
typedef struct{
|
||||
char *logpath; // full path to logfile
|
||||
Cl_loglevel loglevel; // loglevel
|
||||
pthread_mutex_t mutex; // log mutex
|
||||
} Cl_log;
|
||||
|
||||
extern Cl_log *globlog; // global log file
|
||||
|
||||
Cl_log *Cl_createlog(const char *logpath, Cl_loglevel level);
|
||||
#define OPENLOG(nm, lvl) (globlog = Cl_createlog(nm, lvl))
|
||||
void Cl_deletelog(Cl_log **log);
|
||||
int Cl_putlogt(int timest, Cl_log *log, Cl_loglevel lvl, const char *fmt, ...);
|
||||
// shortcuts for different log levels; ..ADD - add message without timestamp
|
||||
#define LOGERR(...) do{Cl_putlogt(1, globlog, LOGLEVEL_ERR, __VA_ARGS__);}while(0)
|
||||
#define LOGERRADD(...) do{Cl_putlogt(1, globlog, LOGLEVEL_ERR, __VA_ARGS__);}while(0)
|
||||
#define LOGWARN(...) do{Cl_putlogt(1, globlog, LOGLEVEL_WARN, __VA_ARGS__);}while(0)
|
||||
#define LOGWARNADD(...) do{Cl_putlogt(0, globlog, LOGLEVEL_WARN, __VA_ARGS__);}while(0)
|
||||
#define LOGMSG(...) do{Cl_putlogt(1, globlog, LOGLEVEL_MSG, __VA_ARGS__);}while(0)
|
||||
#define LOGMSGADD(...) do{Cl_putlogt(0, globlog, LOGLEVEL_MSG, __VA_ARGS__);}while(0)
|
||||
#define LOGDBG(...) do{Cl_putlogt(1, globlog, LOGLEVEL_DBG, __VA_ARGS__);}while(0)
|
||||
#define LOGDBGADD(...) do{Cl_putlogt(0, globlog, LOGLEVEL_DBG, __VA_ARGS__);}while(0)
|
||||
|
||||
char *find_device();
|
||||
|
||||
int str2long(char *str, long* l);
|
||||
|
||||
@ -67,12 +67,12 @@ int main(int argc, char **argv){
|
||||
signal(SIGTSTP, SIG_IGN); // ignore ctrl+Z
|
||||
|
||||
if(GP->logfile){
|
||||
Cl_loglevel lvl = LOGLEVEL_ERR; // default log level - errors
|
||||
sl_loglevel lvl = LOGLEVEL_ERR; // default log level - errors
|
||||
int v = GP->verb;
|
||||
while(v--){ // increase loglevel for each "-v"
|
||||
if(++lvl == LOGLEVEL_ANY) break;
|
||||
}
|
||||
OPENLOG(GP->logfile, lvl);
|
||||
OPENLOG(GP->logfile, lvl, 1);
|
||||
}
|
||||
#ifndef EBUG
|
||||
if(daemon(1, 0)){
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user