fixed logs

This commit is contained in:
Edward Emelianov 2020-11-06 16:06:35 +03:00
parent 9081ca21a0
commit f410c34c10
6 changed files with 60 additions and 110 deletions

View File

@ -26,9 +26,10 @@
#include <sys/ipc.h>
#include <sys/msg.h>
#include "usefull_macros.h"
#include "canmsg.h"
#include "can_io.h"
#include "checkfile.h"
char can_dev[40] = "/dev/can0";
int can_fd=-1;
@ -110,8 +111,7 @@ void *init_can_io() { /* returns shared area addr. for client control process*/
memcpy(can_shm_key.name, p+1, 4);
can_shm_key.name[4]='\0';
}else{
fprintf(stderr,"Wrong CAN device name: %s\n", can_dev);
exit(1);
ERRX("Wrong CAN device name: %s\n", can_dev);
}
can_shm_id = shmget(can_shm_key.code, CAN_SHM_SIZE, 0644);
if(can_shm_id<0 && errno==EACCES)
@ -123,22 +123,18 @@ void *init_can_io() { /* returns shared area addr. for client control process*/
if(can_shm_id<0){
can_prtime(stderr);
if(new_shm)
sprintf(msg,"Can't create shm CAN buffer '%s'",can_shm_key.name);
ERR("Can't create shm CAN buffer '%s'",can_shm_key.name);
else if(server_mode)
sprintf(msg,"CAN-I/O: Can't find shm segment for CAN buffer '%s'",can_shm_key.name);
ERR("CAN-I/O: Can't find shm segment for CAN buffer '%s'",can_shm_key.name);
else
snprintf(msg, 256, "Can't find shm segment for CAN buffer '%s' (maybe no CAN-I/O process?)",can_shm_key.name);
perror(msg);
exit(errno);
ERR("Can't find shm segment for CAN buffer '%s' (maybe no CAN-I/O process?)",can_shm_key.name);
}
can_shm_addr = shmat(can_shm_id, NULL, 0);
if(can_shm_addr == (void*)-1 && errno == EACCES)
can_shm_addr = shmat(can_shm_id, NULL, SHM_RDONLY);
if(can_shm_addr == (void*)-1){
sprintf(msg,"Can't attach shm CAN buffer '%s'",can_shm_key.name);
perror(msg);
shmctl(can_shm_id, IPC_RMID, NULL);
exit(errno);
ERR("Can't attach shm CAN buffer '%s'",can_shm_key.name);
}
}
can_ctrl_addr = (canmsg_t *)(can_shm_addr+sizeof(int)*4);
@ -158,18 +154,15 @@ void *init_can_io() { /* returns shared area addr. for client control process*/
can_fd = open(can_dev, flags);
canout.id = msgget(canout.key.code, canout.mode);
if(can_fd < 0 && canout.id < 0) {
snprintf(msg, 256, "Error opening CAN device(%s) or CAN output queue '%s' (maybe no CANqueue server process?)",can_dev,canout.key.name);
perror(msg);
WARNX("Error opening CAN device(%s) or CAN output queue '%s' (maybe no CANqueue server process?)",can_dev,canout.key.name);
}
}
}
if(can_lk > 0) close(can_lk);
if(( can_lk = open(can_lck, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH )) < 0 ){
sprintf(msg,"Error opening CAN device lock-file %s", can_lck);
perror(msg);
shmctl(can_shm_id, IPC_RMID, NULL);
close(can_fd);
exit(errno);
ERR("Error opening CAN device lock-file %s", can_lck);
}
fchmod(can_lk, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
if(new_shm){
@ -243,23 +236,11 @@ void *start_can_io(_U_ void *arg){
if(n < 0){
perror("CAN Rx error");
} else if(n > 0) {
/* work around the timestamp bug in old driver version
while((double)rx.timestamp.tv_sec+(double)rx.timestamp.tv_usec/1e6 < (double)tm.tv_sec+(double)tm.tv_usec/1e6) {
rx.timestamp.tv_usec += 10000;
if(rx.timestamp.tv_usec > 1000000) {
rx.timestamp.tv_sec++;
rx.timestamp.tv_usec -= 1000000;
}
}*/
if(flock(can_lk, LOCK_EX)<0) perror("locking CAN");
rx_buff[rx_buff_pntr] = rx;
rx_buff_pntr = (rx_buff_pntr + 1) % CAN_RX_SIZE;
if(flock(can_lk, LOCK_UN)<0) perror("unlocking CAN");
//fprintf(stderr,"%d read(id=%02x,len=%d)\n",rx_buff_pntr,rx.id,rx.length);fflush(stderr);
/*fprintf(stderr,"reading CAN: 1 frame\n");*/
}/*else {
* fprintf(stderr,"reading CAN: nothing\n");fflush(stderr);
} */
}
} while(n>0);
}
}
@ -283,7 +264,6 @@ void can_put_buff_frame(double rtime, int id, int length, unsigned char data[])
rx_buff[rx_buff_pntr] = rx;
rx_buff_pntr = (rx_buff_pntr + 1) % CAN_RX_SIZE;
if(flock(can_lk, LOCK_UN)<0) perror("unlocking CAN");
//fprintf(stderr,"put_buf(id=%02x,flag=%1x,len=%d)\n",rx.id,rx.flags,rx.length);fflush(stderr);
}
/* ÷ÓÅ ÎÏÒÍÁÌØÎÏ Ó SHM-ÂÕÆÅÒÏÍ CAN-I/O ÐÒÏÃÅÓÓÁ */
@ -481,6 +461,7 @@ void can_exit(int sig) {
struct shmid_ds buf;
if(sig) signal(sig,SIG_IGN);
putlog("Received signal %d\n", sig);
if(server_mode) can_abort(sig);
switch (sig) {
case 0 : strcpy(ss,"Exiting - "); break;
@ -517,6 +498,7 @@ void can_exit(int sig) {
shmctl(can_shm_id, IPC_STAT, &buf);
if(buf.shm_nattch == 0)
shmctl(can_shm_id, IPC_RMID, NULL);
unlink_pidfile();
exit(sig);
}
}

View File

@ -5,6 +5,8 @@
#ifndef CAN_IO_H__
#define CAN_IO_H__
#include <stdio.h>
#define _U_ __attribute__((__unused__))

View File

@ -20,6 +20,7 @@
#include <sys/wait.h>
#include <sys/prctl.h>
#include <signal.h>
#include "can_io.h"
#include "can_encoder.h"
#include "canopen.h"
#include "checkfile.h"
@ -45,14 +46,8 @@ int verbose(const char *fmt, ...){
}
static pid_t childpid;
/**
* @brief signals - signal handler (also called by functions ERR/ERRX)
* @param signo - signal number
*/
void signals(int signo){
WARNX("Received signal %d", signo);
if(childpid) unlink_pidfile(); // parent process died
exit(signo);
can_exit(signo);
}
static void cmdparser(){
@ -91,22 +86,21 @@ int main (int argc, char *argv[]){
}
}
signal(SIGTERM, signals);
signal(SIGKILL, signals);
signal(SIGTSTP, SIG_IGN);
signal(SIGHUP, SIG_IGN);
can_dev[8] = '1';
/*
if(G->server || G->standalone){ // init hardware
check4running(G->pidfilename);
}
*/
if(G->server){ // daemonize & run server
#if !defined EBUG
if(daemon(1, 0)){
ERR("daemon()");
};
#endif
check4running(G->pidfilename);
while(1){ // guard for dead processes
childpid = fork();
if(childpid){
@ -132,6 +126,8 @@ int main (int argc, char *argv[]){
return 0;
}
check4running(G->pidfilename);
if(!G->noencoder && init_encoder(G->nodenum, G->reset)){
WARNX("Encoder not found");
#ifndef EBUG

View File

@ -293,7 +293,7 @@ static void *handle_socket(void *asock){
msg = S_STATUS_FORBIDDEN;
break;
default:
"Unknown status";
msg = "Unknown status";
}
sprintf(buff, "%s", msg);
}else sprintf(buff, S_ANS_ERR);
@ -312,7 +312,7 @@ static void *handle_socket(void *asock){
static void *server(void *asock){
int sock = *((int*)asock);
if(listen(sock, BACKLOG) == -1){
WARN("listen() failed");
//WARN("listen() failed");
return NULL;
}
while(1){
@ -349,6 +349,7 @@ static void subst_file(char *name){
snprintf(aname, l, "%sXXXXXX", name);
int fd = mkstemp(aname);
if(fd < 0) goto ret;
fchmod(fd, 0644);
FILE *f = fdopen(fd, "w");
if(!f) goto ret;
fprintf(f, "FOCUS = %.2f\n", curPos());
@ -370,7 +371,7 @@ static void daemon_(int sock){
}
do{
if(pthread_kill(sock_thread, 0) == ESRCH){ // died
WARNX("sockets thread died");
//WARNX("sockets thread died");
pthread_join(sock_thread, NULL);
if(pthread_create(&sock_thread, NULL, server, (void*) &sock)){
ERR("new pthread_create() failed");

View File

@ -20,6 +20,7 @@
*/
#include "usefull_macros.h"
#include <pthread.h>
#include <time.h>
#include <linux/limits.h> // PATH_MAX
@ -387,86 +388,53 @@ int str2double(double *num, const char *str){
return TRUE;
}
static FILE *Flog = NULL; // log file descriptor
static char *logname = NULL; // full logfile name (with PID prefix)
static time_t log_open_time = 0; // time when log file was opened
static pthread_mutex_t logmutex = PTHREAD_MUTEX_INITIALIZER;
static char *logname = NULL;
/**
* Try to open log file
* if failed show warning message
*/
void openlogfile(char *name){
//char buf[PATH_MAX];
if(Flog){
fclose(Flog);
Flog = NULL;
}
if(!name) return;
/*
if(!name){ // filename is omit -> try to open log with old name
if(!fullogname){
WARNX(_("openlogfile(): need filename"));
FILE *logfd = fopen(name, "a");
if(!logfd){
WARN("Can't open log file");
return;
}
name = fullogname;
}else{
if(fullogname) FREE(fullogname);
// append PID to name
snprintf(buf, PATH_MAX, "%d_%s", getpid(), name);
name = buf;
green(_("Try to open log file %s in append mode"), name);
printf("\n");
fclose(logfd);
/*if(pthread_mutex_init(&logmutex, NULL)){
WARN("Can't init log mutes");
return;
}*/
if(!(Flog = fopen(name, "a"))){
WARN(_("Can't open log file"));
return;
}
log_open_time = time(NULL);
logname = name;
//fullogname = strdup(buf);
FREE(logname);
logname = strdup(name);
}
/**
* Save message to log file, rotate logs every 24 hours
*/
int putlog(const char *fmt, ...){
if(!Flog) return 0;
int putlogst(int timest, const char *fmt, ...){
if(pthread_mutex_lock(&logmutex)){
WARN("Can't lock log mutex");
return 0;
}
int i = 0;
FILE *logfd = fopen(logname, "a");
if(!logfd) goto rtn;
if(timest){
char strtm[128];
time_t t = time(NULL);
struct tm *curtm = localtime(&t);
int i = strftime(strtm, 128, "%Y/%m/%d-%H:%M", curtm);
if(t - log_open_time > 86400){ // rotate log
fprintf(Flog, "\n\n%s\tRotate log\n", strtm);
fclose(Flog);
Flog = NULL;
char newname[PATH_MAX];
snprintf(newname, PATH_MAX, "%s.old", logname);
if(rename(logname, newname)) WARN("rename()");
openlogfile(NULL);
if(!Flog) return 0;
}
strftime(strtm, 128, "%Y/%m/%d-%H:%M:%S", curtm);
i = fprintf(logfd, "%s\t", strtm);
}else i += fprintf(logfd, "\t\t\t");
va_list ar;
fprintf(Flog, "%s\t", strtm);
va_start(ar, fmt);
i += vfprintf(Flog, fmt, ar);
i += vfprintf(logfd, fmt, ar);
va_end(ar);
fprintf(Flog, "\n");
++i;
fflush(Flog);
return i;
}
// add message to log file without printing time
int addtolog(const char *fmt, ...){
if(!Flog) return 0;
va_list ar;
int i = 1;
fprintf(Flog, "\t\t\t");
va_start(ar, fmt);
i += vfprintf(Flog, fmt, ar);
va_end(ar);
fprintf(Flog, "\n");
++i;
fflush(Flog);
i += fprintf(logfd, "\n");
fclose(logfd);
rtn:
pthread_mutex_unlock(&logmutex);
return i;
}

View File

@ -154,8 +154,9 @@ int write_tty(char *buff, size_t length);
int str2double(double *num, const char *str);
void openlogfile(char *name);
int putlog(const char *fmt, ...);
int addtolog(const char *fmt, ...);
int putlogst(int timest, const char *fmt, ...);
#define putlog(...) putlogst(1, __VA_ARGS__)
#define addtolog(...) putlogst(0, __VA_ARGS__)
void warnsingle(const char *msg, locwarn errnum);
void clrwarnsingle(locwarn errnum);
#endif // __USEFULL_MACROS_H__