mirror of
https://github.com/eddyem/zeiss_utils.git
synced 2025-12-06 10:45:17 +03:00
fixed logs
This commit is contained in:
parent
9081ca21a0
commit
f410c34c10
@ -26,9 +26,10 @@
|
|||||||
#include <sys/ipc.h>
|
#include <sys/ipc.h>
|
||||||
#include <sys/msg.h>
|
#include <sys/msg.h>
|
||||||
|
|
||||||
|
#include "usefull_macros.h"
|
||||||
#include "canmsg.h"
|
#include "canmsg.h"
|
||||||
#include "can_io.h"
|
#include "can_io.h"
|
||||||
|
#include "checkfile.h"
|
||||||
|
|
||||||
char can_dev[40] = "/dev/can0";
|
char can_dev[40] = "/dev/can0";
|
||||||
int can_fd=-1;
|
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);
|
memcpy(can_shm_key.name, p+1, 4);
|
||||||
can_shm_key.name[4]='\0';
|
can_shm_key.name[4]='\0';
|
||||||
}else{
|
}else{
|
||||||
fprintf(stderr,"Wrong CAN device name: %s\n", can_dev);
|
ERRX("Wrong CAN device name: %s\n", can_dev);
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
can_shm_id = shmget(can_shm_key.code, CAN_SHM_SIZE, 0644);
|
can_shm_id = shmget(can_shm_key.code, CAN_SHM_SIZE, 0644);
|
||||||
if(can_shm_id<0 && errno==EACCES)
|
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){
|
if(can_shm_id<0){
|
||||||
can_prtime(stderr);
|
can_prtime(stderr);
|
||||||
if(new_shm)
|
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)
|
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
|
else
|
||||||
snprintf(msg, 256, "Can't find shm segment for CAN buffer '%s' (maybe no CAN-I/O process?)",can_shm_key.name);
|
ERR("Can't find shm segment for CAN buffer '%s' (maybe no CAN-I/O process?)",can_shm_key.name);
|
||||||
perror(msg);
|
|
||||||
exit(errno);
|
|
||||||
}
|
}
|
||||||
can_shm_addr = shmat(can_shm_id, NULL, 0);
|
can_shm_addr = shmat(can_shm_id, NULL, 0);
|
||||||
if(can_shm_addr == (void*)-1 && errno == EACCES)
|
if(can_shm_addr == (void*)-1 && errno == EACCES)
|
||||||
can_shm_addr = shmat(can_shm_id, NULL, SHM_RDONLY);
|
can_shm_addr = shmat(can_shm_id, NULL, SHM_RDONLY);
|
||||||
if(can_shm_addr == (void*)-1){
|
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);
|
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);
|
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);
|
can_fd = open(can_dev, flags);
|
||||||
canout.id = msgget(canout.key.code, canout.mode);
|
canout.id = msgget(canout.key.code, canout.mode);
|
||||||
if(can_fd < 0 && canout.id < 0) {
|
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);
|
WARNX("Error opening CAN device(%s) or CAN output queue '%s' (maybe no CANqueue server process?)",can_dev,canout.key.name);
|
||||||
perror(msg);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(can_lk > 0) close(can_lk);
|
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 ){
|
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);
|
shmctl(can_shm_id, IPC_RMID, NULL);
|
||||||
close(can_fd);
|
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);
|
fchmod(can_lk, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
|
||||||
if(new_shm){
|
if(new_shm){
|
||||||
@ -243,23 +236,11 @@ void *start_can_io(_U_ void *arg){
|
|||||||
if(n < 0){
|
if(n < 0){
|
||||||
perror("CAN Rx error");
|
perror("CAN Rx error");
|
||||||
} else if(n > 0) {
|
} 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");
|
if(flock(can_lk, LOCK_EX)<0) perror("locking CAN");
|
||||||
rx_buff[rx_buff_pntr] = rx;
|
rx_buff[rx_buff_pntr] = rx;
|
||||||
rx_buff_pntr = (rx_buff_pntr + 1) % CAN_RX_SIZE;
|
rx_buff_pntr = (rx_buff_pntr + 1) % CAN_RX_SIZE;
|
||||||
if(flock(can_lk, LOCK_UN)<0) perror("unlocking CAN");
|
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);
|
} 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[rx_buff_pntr] = rx;
|
||||||
rx_buff_pntr = (rx_buff_pntr + 1) % CAN_RX_SIZE;
|
rx_buff_pntr = (rx_buff_pntr + 1) % CAN_RX_SIZE;
|
||||||
if(flock(can_lk, LOCK_UN)<0) perror("unlocking CAN");
|
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 ÐÒÏÃÅÓÓÁ */
|
/* ÷ÓÅ ÎÏÒÍÁÌØÎÏ Ó SHM-ÂÕÆÅÒÏÍ CAN-I/O ÐÒÏÃÅÓÓÁ */
|
||||||
@ -481,6 +461,7 @@ void can_exit(int sig) {
|
|||||||
|
|
||||||
struct shmid_ds buf;
|
struct shmid_ds buf;
|
||||||
if(sig) signal(sig,SIG_IGN);
|
if(sig) signal(sig,SIG_IGN);
|
||||||
|
putlog("Received signal %d\n", sig);
|
||||||
if(server_mode) can_abort(sig);
|
if(server_mode) can_abort(sig);
|
||||||
switch (sig) {
|
switch (sig) {
|
||||||
case 0 : strcpy(ss,"Exiting - "); break;
|
case 0 : strcpy(ss,"Exiting - "); break;
|
||||||
@ -517,6 +498,7 @@ void can_exit(int sig) {
|
|||||||
shmctl(can_shm_id, IPC_STAT, &buf);
|
shmctl(can_shm_id, IPC_STAT, &buf);
|
||||||
if(buf.shm_nattch == 0)
|
if(buf.shm_nattch == 0)
|
||||||
shmctl(can_shm_id, IPC_RMID, NULL);
|
shmctl(can_shm_id, IPC_RMID, NULL);
|
||||||
|
unlink_pidfile();
|
||||||
exit(sig);
|
exit(sig);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,6 +5,8 @@
|
|||||||
#ifndef CAN_IO_H__
|
#ifndef CAN_IO_H__
|
||||||
#define CAN_IO_H__
|
#define CAN_IO_H__
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
#define _U_ __attribute__((__unused__))
|
#define _U_ __attribute__((__unused__))
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -20,6 +20,7 @@
|
|||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
#include <sys/prctl.h>
|
#include <sys/prctl.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
|
#include "can_io.h"
|
||||||
#include "can_encoder.h"
|
#include "can_encoder.h"
|
||||||
#include "canopen.h"
|
#include "canopen.h"
|
||||||
#include "checkfile.h"
|
#include "checkfile.h"
|
||||||
@ -45,14 +46,8 @@ int verbose(const char *fmt, ...){
|
|||||||
}
|
}
|
||||||
|
|
||||||
static pid_t childpid;
|
static pid_t childpid;
|
||||||
/**
|
|
||||||
* @brief signals - signal handler (also called by functions ERR/ERRX)
|
|
||||||
* @param signo - signal number
|
|
||||||
*/
|
|
||||||
void signals(int signo){
|
void signals(int signo){
|
||||||
WARNX("Received signal %d", signo);
|
can_exit(signo);
|
||||||
if(childpid) unlink_pidfile(); // parent process died
|
|
||||||
exit(signo);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void cmdparser(){
|
static void cmdparser(){
|
||||||
@ -91,22 +86,21 @@ int main (int argc, char *argv[]){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
signal(SIGTERM, signals);
|
|
||||||
signal(SIGKILL, signals);
|
|
||||||
signal(SIGTSTP, SIG_IGN);
|
signal(SIGTSTP, SIG_IGN);
|
||||||
signal(SIGHUP, SIG_IGN);
|
signal(SIGHUP, SIG_IGN);
|
||||||
can_dev[8] = '1';
|
can_dev[8] = '1';
|
||||||
|
/*
|
||||||
if(G->server || G->standalone){ // init hardware
|
if(G->server || G->standalone){ // init hardware
|
||||||
check4running(G->pidfilename);
|
check4running(G->pidfilename);
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
if(G->server){ // daemonize & run server
|
if(G->server){ // daemonize & run server
|
||||||
#if !defined EBUG
|
#if !defined EBUG
|
||||||
if(daemon(1, 0)){
|
if(daemon(1, 0)){
|
||||||
ERR("daemon()");
|
ERR("daemon()");
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
check4running(G->pidfilename);
|
||||||
while(1){ // guard for dead processes
|
while(1){ // guard for dead processes
|
||||||
childpid = fork();
|
childpid = fork();
|
||||||
if(childpid){
|
if(childpid){
|
||||||
@ -132,6 +126,8 @@ int main (int argc, char *argv[]){
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
check4running(G->pidfilename);
|
||||||
|
|
||||||
if(!G->noencoder && init_encoder(G->nodenum, G->reset)){
|
if(!G->noencoder && init_encoder(G->nodenum, G->reset)){
|
||||||
WARNX("Encoder not found");
|
WARNX("Encoder not found");
|
||||||
#ifndef EBUG
|
#ifndef EBUG
|
||||||
|
|||||||
@ -293,7 +293,7 @@ static void *handle_socket(void *asock){
|
|||||||
msg = S_STATUS_FORBIDDEN;
|
msg = S_STATUS_FORBIDDEN;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
"Unknown status";
|
msg = "Unknown status";
|
||||||
}
|
}
|
||||||
sprintf(buff, "%s", msg);
|
sprintf(buff, "%s", msg);
|
||||||
}else sprintf(buff, S_ANS_ERR);
|
}else sprintf(buff, S_ANS_ERR);
|
||||||
@ -312,7 +312,7 @@ static void *handle_socket(void *asock){
|
|||||||
static void *server(void *asock){
|
static void *server(void *asock){
|
||||||
int sock = *((int*)asock);
|
int sock = *((int*)asock);
|
||||||
if(listen(sock, BACKLOG) == -1){
|
if(listen(sock, BACKLOG) == -1){
|
||||||
WARN("listen() failed");
|
//WARN("listen() failed");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
while(1){
|
while(1){
|
||||||
@ -349,6 +349,7 @@ static void subst_file(char *name){
|
|||||||
snprintf(aname, l, "%sXXXXXX", name);
|
snprintf(aname, l, "%sXXXXXX", name);
|
||||||
int fd = mkstemp(aname);
|
int fd = mkstemp(aname);
|
||||||
if(fd < 0) goto ret;
|
if(fd < 0) goto ret;
|
||||||
|
fchmod(fd, 0644);
|
||||||
FILE *f = fdopen(fd, "w");
|
FILE *f = fdopen(fd, "w");
|
||||||
if(!f) goto ret;
|
if(!f) goto ret;
|
||||||
fprintf(f, "FOCUS = %.2f\n", curPos());
|
fprintf(f, "FOCUS = %.2f\n", curPos());
|
||||||
@ -370,7 +371,7 @@ static void daemon_(int sock){
|
|||||||
}
|
}
|
||||||
do{
|
do{
|
||||||
if(pthread_kill(sock_thread, 0) == ESRCH){ // died
|
if(pthread_kill(sock_thread, 0) == ESRCH){ // died
|
||||||
WARNX("sockets thread died");
|
//WARNX("sockets thread died");
|
||||||
pthread_join(sock_thread, NULL);
|
pthread_join(sock_thread, NULL);
|
||||||
if(pthread_create(&sock_thread, NULL, server, (void*) &sock)){
|
if(pthread_create(&sock_thread, NULL, server, (void*) &sock)){
|
||||||
ERR("new pthread_create() failed");
|
ERR("new pthread_create() failed");
|
||||||
|
|||||||
@ -20,6 +20,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "usefull_macros.h"
|
#include "usefull_macros.h"
|
||||||
|
#include <pthread.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <linux/limits.h> // PATH_MAX
|
#include <linux/limits.h> // PATH_MAX
|
||||||
|
|
||||||
@ -387,86 +388,53 @@ int str2double(double *num, const char *str){
|
|||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
static FILE *Flog = NULL; // log file descriptor
|
static pthread_mutex_t logmutex = PTHREAD_MUTEX_INITIALIZER;
|
||||||
static char *logname = NULL; // full logfile name (with PID prefix)
|
static char *logname = NULL;
|
||||||
static time_t log_open_time = 0; // time when log file was opened
|
|
||||||
/**
|
/**
|
||||||
* Try to open log file
|
* Try to open log file
|
||||||
* if failed show warning message
|
* if failed show warning message
|
||||||
*/
|
*/
|
||||||
void openlogfile(char *name){
|
void openlogfile(char *name){
|
||||||
//char buf[PATH_MAX];
|
FILE *logfd = fopen(name, "a");
|
||||||
if(Flog){
|
if(!logfd){
|
||||||
fclose(Flog);
|
WARN("Can't open log file");
|
||||||
Flog = NULL;
|
|
||||||
}
|
|
||||||
if(!name) return;
|
|
||||||
/*
|
|
||||||
if(!name){ // filename is omit -> try to open log with old name
|
|
||||||
if(!fullogname){
|
|
||||||
WARNX(_("openlogfile(): need filename"));
|
|
||||||
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");
|
|
||||||
}*/
|
|
||||||
if(!(Flog = fopen(name, "a"))){
|
|
||||||
WARN(_("Can't open log file"));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
log_open_time = time(NULL);
|
fclose(logfd);
|
||||||
logname = name;
|
/*if(pthread_mutex_init(&logmutex, NULL)){
|
||||||
//fullogname = strdup(buf);
|
WARN("Can't init log mutes");
|
||||||
|
return;
|
||||||
|
}*/
|
||||||
|
FREE(logname);
|
||||||
|
logname = strdup(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Save message to log file, rotate logs every 24 hours
|
* Save message to log file, rotate logs every 24 hours
|
||||||
*/
|
*/
|
||||||
int putlog(const char *fmt, ...){
|
int putlogst(int timest, const char *fmt, ...){
|
||||||
if(!Flog) return 0;
|
if(pthread_mutex_lock(&logmutex)){
|
||||||
char strtm[128];
|
WARN("Can't lock log mutex");
|
||||||
time_t t = time(NULL);
|
return 0;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
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);
|
||||||
|
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;
|
va_list ar;
|
||||||
fprintf(Flog, "%s\t", strtm);
|
|
||||||
va_start(ar, fmt);
|
va_start(ar, fmt);
|
||||||
i += vfprintf(Flog, fmt, ar);
|
i += vfprintf(logfd, fmt, ar);
|
||||||
va_end(ar);
|
va_end(ar);
|
||||||
fprintf(Flog, "\n");
|
i += fprintf(logfd, "\n");
|
||||||
++i;
|
fclose(logfd);
|
||||||
fflush(Flog);
|
rtn:
|
||||||
return i;
|
pthread_mutex_unlock(&logmutex);
|
||||||
}
|
|
||||||
|
|
||||||
// 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);
|
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -154,8 +154,9 @@ int write_tty(char *buff, size_t length);
|
|||||||
int str2double(double *num, const char *str);
|
int str2double(double *num, const char *str);
|
||||||
|
|
||||||
void openlogfile(char *name);
|
void openlogfile(char *name);
|
||||||
int putlog(const char *fmt, ...);
|
int putlogst(int timest, const char *fmt, ...);
|
||||||
int addtolog(const char *fmt, ...);
|
#define putlog(...) putlogst(1, __VA_ARGS__)
|
||||||
|
#define addtolog(...) putlogst(0, __VA_ARGS__)
|
||||||
void warnsingle(const char *msg, locwarn errnum);
|
void warnsingle(const char *msg, locwarn errnum);
|
||||||
void clrwarnsingle(locwarn errnum);
|
void clrwarnsingle(locwarn errnum);
|
||||||
#endif // __USEFULL_MACROS_H__
|
#endif // __USEFULL_MACROS_H__
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user