mirror of
https://github.com/eddyem/small_tel.git
synced 2025-12-06 02:35:14 +03:00
Add Weather_chk
This commit is contained in:
parent
b0bba12a9d
commit
2c8b8d97df
69
Auxiliary_utils/Weather_chk/CMakeLists.txt
Normal file
69
Auxiliary_utils/Weather_chk/CMakeLists.txt
Normal file
@ -0,0 +1,69 @@
|
||||
cmake_minimum_required(VERSION 3.0)
|
||||
set(PROJ chkweather)
|
||||
set(MINOR_VERSION "1")
|
||||
set(MID_VERSION "0")
|
||||
set(MAJOR_VERSION "0")
|
||||
set(VERSION "${MAJOR_VERSION}.${MID_VERSION}.${MINOR_VERSION}")
|
||||
|
||||
project(${PROJ} VERSION ${PROJ_VERSION} LANGUAGES C)
|
||||
#enable_language(C)
|
||||
|
||||
message("VER: ${VERSION}")
|
||||
|
||||
# default flags
|
||||
set(CMAKE_C_FLAGS_RELEASE "")
|
||||
set(CMAKE_C_FLAGS_DEBUG "")
|
||||
set(CMAKE_C_FLAGS "-O2 -std=gnu99")
|
||||
|
||||
set(CMAKE_COLOR_MAKEFILE ON)
|
||||
|
||||
# here is one of two variants: all .c in directory or .c files in list
|
||||
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} SOURCES)
|
||||
|
||||
# cmake -DDEBUG=1 -> debugging
|
||||
if(DEFINED EBUG)
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wextra -Wall -Werror -W")
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wextra -Wall -Werror -W")
|
||||
set(CMAKE_BUILD_TYPE DEBUG)
|
||||
set(CMAKE_VERBOSE_MAKEFILE "ON")
|
||||
add_definitions(-DEBUG)
|
||||
else()
|
||||
set(CMAKE_BUILD_TYPE RELEASE)
|
||||
endif()
|
||||
|
||||
###### pkgconfig ######
|
||||
# pkg-config modules (for pkg-check-modules)
|
||||
set(MODULES usefull_macros)
|
||||
|
||||
# find packages:
|
||||
find_package(PkgConfig REQUIRED)
|
||||
pkg_check_modules(${PROJ} REQUIRED ${MODULES})
|
||||
|
||||
###### additional flags ######
|
||||
#list(APPEND ${PROJ}_LIBRARIES "-lfftw3_threads")
|
||||
|
||||
# change wrong behaviour with install prefix
|
||||
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT AND CMAKE_INSTALL_PREFIX MATCHES "/usr/local")
|
||||
else()
|
||||
message("Change default install path to /usr/local")
|
||||
set(CMAKE_INSTALL_PREFIX "/usr/local")
|
||||
endif()
|
||||
message("Install dir prefix: ${CMAKE_INSTALL_PREFIX}")
|
||||
|
||||
# exe file
|
||||
add_executable(${PROJ} ${SOURCES})
|
||||
# -I
|
||||
include_directories(${${PROJ}_INCLUDE_DIRS})
|
||||
# -L
|
||||
link_directories(${${PROJ}_LIBRARY_DIRS})
|
||||
# -D
|
||||
add_definitions(${CFLAGS} -DLOCALEDIR=\"${LOCALEDIR}\"
|
||||
-DPACKAGE_VERSION=\"${VERSION}\" -DGETTEXT_PACKAGE=\"${PROJ}\"
|
||||
-DMINOR_VERSION=\"${MINOR_VERSION}\" -DMID_VERSION=\"${MID_VERSION}\"
|
||||
-DMAJOR_VERSION=\"${MAJOR_VESION}\")
|
||||
|
||||
# -l
|
||||
target_link_libraries(${PROJ} ${${PROJ}_LIBRARIES} -lm)
|
||||
|
||||
# Installation of the program
|
||||
INSTALL(TARGETS ${PROJ} DESTINATION "bin")
|
||||
18
Auxiliary_utils/Weather_chk/Readme.md
Normal file
18
Auxiliary_utils/Weather_chk/Readme.md
Normal file
@ -0,0 +1,18 @@
|
||||
Check meteostation parameters
|
||||
===========================
|
||||
|
||||
Usage: chkweather [args]
|
||||
|
||||
Where args are:
|
||||
|
||||
-d, --devname=arg serial device name
|
||||
-h, --help show this help
|
||||
-s, --speed=arg baudrate (default: 9600)
|
||||
|
||||
|
||||
Output:
|
||||
Rain=0/1
|
||||
Clouds=0/1
|
||||
|
||||
Return value:
|
||||
0 if no rain, 1 if there's rainy
|
||||
73
Auxiliary_utils/Weather_chk/cmdlnopts.c
Normal file
73
Auxiliary_utils/Weather_chk/cmdlnopts.c
Normal file
@ -0,0 +1,73 @@
|
||||
/*
|
||||
* This file is part of the ttyterm project.
|
||||
* Copyright 2020 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#include <assert.h> // assert
|
||||
#include <stdio.h> // printf
|
||||
#include <string.h> // memcpy
|
||||
#include <usefull_macros.h>
|
||||
#include "cmdlnopts.h"
|
||||
|
||||
/*
|
||||
* here are global parameters initialisation
|
||||
*/
|
||||
static int help;
|
||||
static glob_pars G;
|
||||
|
||||
// DEFAULTS
|
||||
// default global parameters
|
||||
glob_pars const Gdefault = {
|
||||
.speed = 9600,
|
||||
.ttyname = "/dev/ttyUSB0",
|
||||
};
|
||||
|
||||
/*
|
||||
* Define command line options by filling structure:
|
||||
* name has_arg flag val type argptr help
|
||||
*/
|
||||
static myoption cmdlnopts[] = {
|
||||
// set 1 to param despite of its repeating number:
|
||||
{"help", NO_ARGS, NULL, 'h', arg_int, APTR(&help), _("show this help")},
|
||||
{"speed", NEED_ARG, NULL, 's', arg_int, APTR(&G.speed), _("baudrate (default: 9600)")},
|
||||
{"devname", NEED_ARG, NULL, 'd', arg_string, APTR(&G.ttyname), _("serial device name")},
|
||||
end_option
|
||||
};
|
||||
|
||||
/**
|
||||
* Parse command line options and return dynamically allocated structure
|
||||
* to global parameters
|
||||
* @param argc - copy of argc from main
|
||||
* @param argv - copy of argv from main
|
||||
* @return allocated structure with global parameters
|
||||
*/
|
||||
glob_pars *parse_args(int argc, char **argv){
|
||||
void *ptr = memcpy(&G, &Gdefault, sizeof(G)); assert(ptr);
|
||||
// format of help: "Usage: progname [args]\n"
|
||||
change_helpstring(_("Usage: %s [args]\n\n\tWhere args are:\n"));
|
||||
// parse arguments
|
||||
parseargs(&argc, &argv, cmdlnopts);
|
||||
if(help) showhelp(-1, cmdlnopts);
|
||||
if(argc > 0){
|
||||
WARNX("Wrong arguments:\n");
|
||||
for(int i = 0; i < argc; i++)
|
||||
fprintf(stderr, "\t%s\n", argv[i]);
|
||||
signals(9);
|
||||
}
|
||||
return &G;
|
||||
}
|
||||
|
||||
36
Auxiliary_utils/Weather_chk/cmdlnopts.h
Normal file
36
Auxiliary_utils/Weather_chk/cmdlnopts.h
Normal file
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* This file is part of the ttyterm project.
|
||||
* Copyright 2020 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#ifndef CMDLNOPTS_H__
|
||||
#define CMDLNOPTS_H__
|
||||
|
||||
/*
|
||||
* here are some typedef's for global data
|
||||
*/
|
||||
typedef struct{
|
||||
int speed; // baudrate
|
||||
char *ttyname; // device name
|
||||
} glob_pars;
|
||||
|
||||
|
||||
// default & global parameters
|
||||
extern glob_pars const Gdefault;
|
||||
|
||||
glob_pars *parse_args(int argc, char **argv);
|
||||
#endif // CMDLNOPTS_H__
|
||||
89
Auxiliary_utils/Weather_chk/main.c
Normal file
89
Auxiliary_utils/Weather_chk/main.c
Normal file
@ -0,0 +1,89 @@
|
||||
/*
|
||||
* This file is part of the ttyterm project.
|
||||
* Copyright 2020 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <ctype.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h> // strcmp
|
||||
#include <usefull_macros.h>
|
||||
#include "cmdlnopts.h"
|
||||
|
||||
#define BUFLEN 2048
|
||||
|
||||
/**
|
||||
* @brief getpar - get parameter value
|
||||
* @param string (i) - string where to search
|
||||
* @param Val (o) - value found
|
||||
* @param Name - parameter name
|
||||
* @return 0 if found
|
||||
*/
|
||||
static int getpar(char *string, double *Val, char *Name){
|
||||
char *p = strstr(string, Name);
|
||||
if(!p) return 1;
|
||||
p += strlen(Name);
|
||||
DBG("search %s: %s", Name, p);
|
||||
if(!Val) return 0;
|
||||
char *endptr;
|
||||
*Val = strtod(p, &endptr);
|
||||
DBG("eptr=%s, val=%g", endptr, *Val);
|
||||
if(endptr == string){
|
||||
WARNX("Double value not found");
|
||||
return 2;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv){
|
||||
glob_pars *G = NULL; // default parameters see in cmdlnopts.c
|
||||
initial_setup();
|
||||
G = parse_args(argc, argv);
|
||||
TTY_descr *dev = new_tty(G->ttyname, G->speed, 64);
|
||||
if(!dev || !(dev = tty_open(dev, 1))) return 1; // open exclusively
|
||||
while(read_tty(dev)); // clear buffer
|
||||
if(write_tty(dev->comfd, "?U\r\n", 3)) ERR("write_tty()");
|
||||
size_t got, L = 0;
|
||||
char buff[BUFLEN], *ptr = buff;
|
||||
double t0 = dtime();
|
||||
while(dtime() - t0 < 1.){ // timeout - 1s
|
||||
got = read_tty(dev);
|
||||
if(got == 0) continue;
|
||||
t0 = dtime();
|
||||
L += got;
|
||||
if(BUFLEN > L){
|
||||
strncpy(ptr, dev->buf, dev->buflen);
|
||||
ptr += got;
|
||||
}else break;
|
||||
}
|
||||
buff[L] = 0;
|
||||
if(L == 0) ERRX("Got nothing from TTY");
|
||||
if(strncmp(buff, "<?U>", 4)) ERRX("Wrong answer: %s", buff);
|
||||
ptr = &buff[4];
|
||||
for(size_t i = 4; i < L; ++i){
|
||||
char c = *ptr;
|
||||
if(isspace(c)) ++ptr;
|
||||
}
|
||||
char *eol = strchr(ptr, '\n');
|
||||
if(eol) *eol = 0;
|
||||
DBG("Now: %s\n", ptr);
|
||||
double rain = 1., clouds = 1.;
|
||||
if(!getpar(ptr, &rain, "RT")) printf("Rain=%g\n", rain);
|
||||
if(!getpar(ptr, &clouds, "WK")) printf("Clouds=%g\n", clouds);
|
||||
close_tty(&dev);
|
||||
if(rain > 0.1) return 1;
|
||||
return 0;
|
||||
}
|
||||
@ -30,7 +30,7 @@ glob_pars *GP;
|
||||
void signals(int signo){
|
||||
restore_console();
|
||||
restore_tty();
|
||||
putlog("exit with status %d", signo);
|
||||
LOG("exit with status %d", signo);
|
||||
exit(signo);
|
||||
}
|
||||
|
||||
@ -49,7 +49,7 @@ int main(int argc, char **argv){
|
||||
signals(0); // never reached!
|
||||
}
|
||||
if(GP->logfile)
|
||||
openlogfile(GP->logfile);
|
||||
Cl_createlog(GP->logfile);
|
||||
#ifndef EBUG
|
||||
if(daemon(1, 0)){
|
||||
ERR("daemon()");
|
||||
@ -57,10 +57,9 @@ int main(int argc, char **argv){
|
||||
while(1){ // guard for dead processes
|
||||
pid_t childpid = fork();
|
||||
if(childpid){
|
||||
putlog("create child with PID %d\n", childpid);
|
||||
LOG("create child with PID %d\n", childpid);
|
||||
DBG("Created child with PID %d\n", childpid);
|
||||
wait(NULL);
|
||||
putlog("child %d died\n", childpid);
|
||||
WARNX("Child %d died\n", childpid);
|
||||
sleep(1);
|
||||
}else{
|
||||
@ -75,7 +74,6 @@ int main(int argc, char **argv){
|
||||
* INSERT CODE HERE
|
||||
* connection check & device validation
|
||||
*/
|
||||
//if(!G->terminal) signals(15); // there's not main controller connected to given terminal
|
||||
daemonize(GP->port);
|
||||
return 0;
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -25,7 +25,6 @@
|
||||
#include "term.h"
|
||||
#include <netdb.h> // addrinfo
|
||||
#include <arpa/inet.h> // inet_ntop
|
||||
#include <pthread.h>
|
||||
#include <limits.h> // INT_xxx
|
||||
#include <signal.h> // pthread_kill
|
||||
#include <unistd.h> // daemon
|
||||
@ -73,7 +72,7 @@ static int waittoread(int sock){
|
||||
}
|
||||
|
||||
/**************** SERVER FUNCTIONS ****************/
|
||||
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
/**
|
||||
* Send data over socket
|
||||
* @param sock - socket fd
|
||||
@ -126,7 +125,7 @@ static char* stringscan(char *str, char *needle){
|
||||
}
|
||||
|
||||
static void *handle_socket(void *asock){
|
||||
//putlog("handle_socket(): getpid: %d, pthread_self: %lu, tid: %lu",getpid(), pthread_self(), syscall(SYS_gettid));
|
||||
//LOG("handle_socket(): getpid: %d, pthread_self: %lu, tid: %lu",getpid(), pthread_self(), syscall(SYS_gettid));
|
||||
FNAME();
|
||||
int sock = *((int*)asock);
|
||||
int webquery = 0; // whether query is web or regular
|
||||
@ -142,13 +141,13 @@ static void *handle_socket(void *asock){
|
||||
continue;
|
||||
}
|
||||
if(!(rd = read(sock, buff, BUFLEN-1))){
|
||||
//putlog("socket closed. Exit");
|
||||
//LOG("socket closed. Exit");
|
||||
break;
|
||||
}
|
||||
//putlog("client send %zd bytes", rd);
|
||||
//LOG("client send %zd bytes", rd);
|
||||
DBG("Got %zd bytes", rd);
|
||||
if(rd < 0){ // error
|
||||
//putlog("some error occured");
|
||||
//LOG("some error occured");
|
||||
DBG("Nothing to read from fd %d (ret: %zd)", sock, rd);
|
||||
break;
|
||||
}
|
||||
@ -166,7 +165,7 @@ static void *handle_socket(void *asock){
|
||||
DBG("user send: %s\nfound=%s", buff, found);
|
||||
if(GP->echo){
|
||||
if(!send_data(sock, webquery, found)){
|
||||
putlog("can't send data, some error occured");
|
||||
LOG("can't send data, some error occured");
|
||||
}
|
||||
}
|
||||
pthread_mutex_lock(&mutex);
|
||||
@ -180,17 +179,17 @@ static void *handle_socket(void *asock){
|
||||
}
|
||||
close(sock);
|
||||
//DBG("closed");
|
||||
//putlog("socket closed, exit");
|
||||
//LOG("socket closed, exit");
|
||||
pthread_exit(NULL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// main socket server
|
||||
static void *server(void *asock){
|
||||
putlog("server(): getpid: %d, pthread_self: %lu, tid: %lu",getpid(), pthread_self(), syscall(SYS_gettid));
|
||||
LOG("server(): getpid: %d, pthread_self: %lu, tid: %lu",getpid(), pthread_self(), syscall(SYS_gettid));
|
||||
int sock = *((int*)asock);
|
||||
if(listen(sock, BACKLOG) == -1){
|
||||
putlog("listen() failed");
|
||||
LOG("listen() failed");
|
||||
WARN("listen");
|
||||
return NULL;
|
||||
}
|
||||
@ -201,7 +200,7 @@ static void *server(void *asock){
|
||||
if(!waittoread(sock)) continue;
|
||||
newsock = accept(sock, (struct sockaddr*)&their_addr, &size);
|
||||
if(newsock <= 0){
|
||||
putlog("accept() failed");
|
||||
LOG("accept() failed");
|
||||
WARN("accept()");
|
||||
continue;
|
||||
}
|
||||
@ -209,18 +208,18 @@ static void *server(void *asock){
|
||||
struct in_addr ipAddr = pV4Addr->sin_addr;
|
||||
char str[INET_ADDRSTRLEN];
|
||||
inet_ntop(AF_INET, &ipAddr, str, INET_ADDRSTRLEN);
|
||||
//putlog("get connection from %s", str);
|
||||
//LOG("get connection from %s", str);
|
||||
DBG("Got connection from %s\n", str);
|
||||
pthread_t handler_thread;
|
||||
if(pthread_create(&handler_thread, NULL, handle_socket, (void*) &newsock)){
|
||||
putlog("server(): pthread_create() failed");
|
||||
LOG("server(): pthread_create() failed");
|
||||
WARN("pthread_create()");
|
||||
}else{
|
||||
DBG("Thread created, detouch");
|
||||
pthread_detach(handler_thread); // don't care about thread state
|
||||
}
|
||||
}
|
||||
putlog("server(): UNREACHABLE CODE REACHED!");
|
||||
LOG("server(): UNREACHABLE CODE REACHED!");
|
||||
}
|
||||
|
||||
// data gathering & socket management
|
||||
@ -228,17 +227,17 @@ static void daemon_(int sock){
|
||||
if(sock < 0) return;
|
||||
pthread_t sock_thread;
|
||||
if(pthread_create(&sock_thread, NULL, server, (void*) &sock)){
|
||||
putlog("daemon_(): pthread_create() failed");
|
||||
LOG("daemon_(): pthread_create() failed");
|
||||
ERR("pthread_create()");
|
||||
}
|
||||
double tgot = 0.;
|
||||
do{
|
||||
if(pthread_kill(sock_thread, 0) == ESRCH){ // died
|
||||
WARNX("Sockets thread died");
|
||||
putlog("Sockets thread died");
|
||||
LOG("Sockets thread died");
|
||||
pthread_join(sock_thread, NULL);
|
||||
if(pthread_create(&sock_thread, NULL, server, (void*) &sock)){
|
||||
putlog("daemon_(): new pthread_create() failed");
|
||||
LOG("daemon_(): new pthread_create() failed");
|
||||
ERR("pthread_create()");
|
||||
}
|
||||
}
|
||||
@ -257,7 +256,7 @@ static void daemon_(int sock){
|
||||
*/
|
||||
pthread_mutex_unlock(&mutex);
|
||||
}while(1);
|
||||
putlog("daemon_(): UNREACHABLE CODE REACHED!");
|
||||
LOG("daemon_(): UNREACHABLE CODE REACHED!");
|
||||
}
|
||||
|
||||
/**
|
||||
@ -295,14 +294,14 @@ void daemonize(char *port){
|
||||
break; // if we get here, we have a successfull connection
|
||||
}
|
||||
if(p == NULL){
|
||||
putlog("failed to bind socket, exit");
|
||||
LOG("failed to bind socket, exit");
|
||||
// looped off the end of the list with no successful bind
|
||||
ERRX("failed to bind socket");
|
||||
}
|
||||
freeaddrinfo(res);
|
||||
daemon_(sock);
|
||||
close(sock);
|
||||
putlog("socket closed, exit");
|
||||
LOG("socket closed, exit");
|
||||
signals(0);
|
||||
}
|
||||
|
||||
|
||||
@ -72,8 +72,8 @@ void try_connect(char *device){
|
||||
char tmpbuf[4096];
|
||||
fflush(stdout);
|
||||
tty_init(device);
|
||||
read_tty(tmpbuf, 4096); // clear rbuf
|
||||
putlog("Connected to %s", device);
|
||||
while(read_tty(tmpbuf, 4096)); // clear rbuf
|
||||
LOG("Connected to %s", device);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -387,48 +387,62 @@ int str2double(double *num, const char *str){
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
FILE *Flog = NULL; // log file descriptor
|
||||
char *logname = NULL;
|
||||
time_t log_open_time = 0;
|
||||
//////////// logging
|
||||
|
||||
static Cl_log log = {0};
|
||||
/**
|
||||
* Try to open log file
|
||||
* if failed show warning message
|
||||
* @brief Cl_createlog - create log file: init mutex, test file open ability
|
||||
* @param log - log structure
|
||||
* @return 0 if all OK
|
||||
*/
|
||||
void openlogfile(char *name){
|
||||
if(!name){
|
||||
WARNX(_("Need filename"));
|
||||
return;
|
||||
int Cl_createlog(char *logname){
|
||||
if(log.logpath){
|
||||
FREE(log.logpath);
|
||||
pthread_mutex_destroy(&log.mutex);
|
||||
}
|
||||
green(_("Try to open log file %s in append mode\n"), name);
|
||||
if(!(Flog = fopen(name, "a"))){
|
||||
WARN(_("Can't open log file"));
|
||||
return;
|
||||
FILE *logfd = fopen(logname, "a");
|
||||
if(!logfd){
|
||||
WARN("Can't open log file");
|
||||
return 2;
|
||||
}
|
||||
log_open_time = time(NULL);
|
||||
logname = name;
|
||||
log.logpath = strdup(logname);
|
||||
fclose(logfd);
|
||||
if(pthread_mutex_init(&log.mutex, NULL)){
|
||||
WARN("Can't init log mutes");
|
||||
return 3;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save message to log file, rotate logs every 24 hours
|
||||
* @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 putlog(const char *fmt, ...){
|
||||
if(!Flog) return 0;
|
||||
time_t t_now = time(NULL);
|
||||
if(t_now - log_open_time > 86400){ // rotate log
|
||||
fprintf(Flog, "\n\t\t%sRotate log\n", ctime(&t_now));
|
||||
fclose(Flog);
|
||||
char newname[PATH_MAX];
|
||||
snprintf(newname, PATH_MAX, "%s.old", logname);
|
||||
if(rename(logname, newname)) WARN("rename()");
|
||||
openlogfile(logname);
|
||||
if(!Flog) return 0;
|
||||
int Cl_putlogt(const char *fmt, ...){
|
||||
if(pthread_mutex_lock(&log.mutex)){
|
||||
WARN("Can't lock log mutex");
|
||||
return 0;
|
||||
}
|
||||
int i = fprintf(Flog, "\n\t\t%s", ctime(&t_now));
|
||||
int i = 0;
|
||||
FILE *logfd = fopen(log.logpath, "a");
|
||||
if(!logfd) goto rtn;
|
||||
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);
|
||||
va_list ar;
|
||||
va_start(ar, fmt);
|
||||
i = vfprintf(Flog, fmt, ar);
|
||||
i += vfprintf(logfd, fmt, ar);
|
||||
va_end(ar);
|
||||
fprintf(Flog, "\n");
|
||||
fflush(Flog);
|
||||
i += fprintf(logfd, "\n");
|
||||
fclose(logfd);
|
||||
rtn:
|
||||
pthread_mutex_unlock(&log.mutex);
|
||||
return i;
|
||||
}
|
||||
|
||||
|
||||
@ -45,6 +45,7 @@
|
||||
#define _(String) (String)
|
||||
#define N_(String) (String)
|
||||
#endif
|
||||
#include <pthread.h>
|
||||
#include <stdlib.h>
|
||||
#include <termios.h>
|
||||
#include <termio.h>
|
||||
@ -76,10 +77,11 @@
|
||||
*/
|
||||
extern int globErr;
|
||||
extern void signals(int sig);
|
||||
#define ERR(...) do{globErr=errno; _WARN(__VA_ARGS__); signals(9);}while(0)
|
||||
#define ERRX(...) do{globErr=0; _WARN(__VA_ARGS__); signals(9);}while(0)
|
||||
#define WARN(...) do{globErr=errno; _WARN(__VA_ARGS__);}while(0)
|
||||
#define WARNX(...) do{globErr=0; _WARN(__VA_ARGS__);}while(0)
|
||||
#define ERR(...) do{globErr=errno; Cl_putlogt(__VA_ARGS__); _WARN(__VA_ARGS__); signals(9);}while(0)
|
||||
#define ERRX(...) do{globErr=0; Cl_putlogt(__VA_ARGS__); _WARN(__VA_ARGS__); signals(9);}while(0)
|
||||
#define WARN(...) do{globErr=errno; Cl_putlogt(__VA_ARGS__); _WARN(__VA_ARGS__);}while(0)
|
||||
#define WARNX(...) do{globErr=0; Cl_putlogt(__VA_ARGS__); _WARN(__VA_ARGS__);}while(0)
|
||||
#define LOG(...) do{Cl_putlogt(__VA_ARGS__); }while(0)
|
||||
|
||||
/*
|
||||
* print function name, debug messages
|
||||
@ -135,6 +137,12 @@ int write_tty(char *buff, size_t length);
|
||||
|
||||
int str2double(double *num, const char *str);
|
||||
|
||||
void openlogfile(char *name);
|
||||
int putlog(const char *fmt, ...);
|
||||
typedef struct{
|
||||
char *logpath; // full path to logfile
|
||||
pthread_mutex_t mutex; // log mutex
|
||||
} Cl_log;
|
||||
|
||||
int Cl_createlog(char *logname);
|
||||
int Cl_putlogt(const char *fmt, ...);
|
||||
|
||||
#endif // __USEFULL_MACROS_H__
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user