static void do_log(const char *message){ int result = 0; pthread_mutex_lock(&_locker); do{ if (_linesize <= _lines){ logger_close(); if (0 != (result = open_logfile(_suffix))){ _fd = STDERR_FILENO; // result = 0 == errno ? EIO : errno; printf("Error!File:%s,Line:%d.Errno:%d,Info:%s.", __FILE__, __LINE__, result,strerror(result)); break; } _lines = 0; } int length = strlen(message); do{ result += write(_fd, message + result, length - result); } while (length != result); _lines++; } while (false); pthread_mutex_unlock(&_locker); return; }
static void proxy_context_free(ProxyContext * const proxy_context) { if (proxy_context == NULL) { return; } options_free(proxy_context); logger_close(proxy_context); }
int log_content_close(log_content_ctx_t **pctx) { int rv = 0; if (!(*pctx) || !(*pctx)->open) return -1; if (logger_close(content_log, *pctx) == -1) { rv = -1; } *pctx = NULL; return rv; }
int logger_cleanup(logger_p log) { if(log) { logger_close(log); #ifdef ENABLE_THREADS pthread_mutex_destroy(&log->file_lock); #endif free(log); return 0; } else { return -1; } }
int main(int argc, char **argv) { printf("Starting Simple Http Server...\n"); struct server_parameters server_parameters; if (parse_arguments(argc, argv, &server_parameters) < 0) { printf("Failed to parse cmd arguments\n"); goto exit; } printf("Server parameters: "); print_server_parameters(&server_parameters); printf("\n"); if (daemonize() < 0) { printf("Failed to become daemon process.\n"); goto exit; } if (logger_init(&logger, LOG_FILE) < 0) { exit(-2); } to_log(&logger, "Simple http server with pid %d started logging.\n", getpid()); /* lets remove dead processes */ struct sigaction sa; sa.sa_handler = sigchld_handler; sigemptyset(&sa.sa_mask); sa.sa_flags = SA_RESTART; if (sigaction(SIGCHLD, &sa, NULL) == -1) { to_log(&logger, "Failed to set up SIGCHLD handler: %d\n", strerror(errno)); goto exit_with_logging; } accept_clients_loop(&server_parameters); sleep(3); to_log(&logger, "Simple http server is stopping.\n"); exit_with_logging: logger_close(&logger); exit: return 0; }
int main(int argc, char *argv[]) { char *conf_file_name = NULL; if (argc == 1) { conf_file_name = CONF_DEFAULT_FILE; } else if (argc == 2) { conf_file_name = argv[1]; } else { fprintf(stderr, "Usage: %s [config_file_name]\n", argv[0]); exit(EXIT_FAILURE); } if (logger_init("deployer.log") != RET_OK) { fprintf(stderr, "logger init failed\n"); exit(EXIT_FAILURE); } Deployer *deployer = deployer_create(deployer_event_cb, NULL); if (deployer == NULL) { fprintf(stderr, "create deployer failed\n"); exit(EXIT_FAILURE); } if (deployer_init(deployer, conf_file_name) != RET_OK) { exit(EXIT_FAILURE); } deployer_do_deploy(deployer); deployer_destroy(deployer); logger_close(); return 0; }
static void cleanup(transport_t t) { transport_close(t); // If something went wrong like a subcontractor crashed, it's possible files will be left behind. // Double check it here char *test_harness_listen_endpoint = g_strdup_printf("%s-%d", TEST_SOCKET, getpid()); char *file = strstr(test_harness_listen_endpoint, IPC_PREFIX); if (file != NULL && (strlen(test_harness_listen_endpoint) > (strlen(IPC_PREFIX) + 1))) { file = test_harness_listen_endpoint + strlen(IPC_PREFIX); struct stat info; if (stat(file, &info) != 0) { debug_log("Couldn't stat %s, not cleaning up.", file); } else { if (unlink(file) != 0) { warning_log("Couldn't delete endpoint %s, may still exist. Error was %i (%s)", file, errno, strerror(errno)); } else { debug_log("Deleted left over IPC end point %s", file); } } } else { debug_log("End point doesn't appear to be ipc. Not removing file: %s ", file); } g_free(test_harness_listen_endpoint); logger_close(); config_close(); }
void server_teardown() { logger_log_message("Shutting down server...", INFO); int i; for (i = 0; i < currentPage; i++) server_page_entry_free(pages[i]); free(pages); for (i = 0; i < totalNumberOfClients; i++) { printf("Closing socket: %d\n", clients[i]->clientSocketId); shutdown(clients[i]->clientSocketId, SHUT_RDWR); close(clients[i]->clientSocketId); client_entry_free(clients[i]); } free(clients); free(mallocsPerClient); free(mallocAddresses); currentPage = -1; currentClient = -1; logger_log_message("Shutting down server finished.", INFO); logger_log_message("Server teardown completed", INFO); logger_close(); }
int logger_open(logger_p log) { struct tm cur_time; char cmd[512]={0x00}; char fname[PATH_MAX_SIZE + NAME_MAX_SIZE + 20]={0x00}; //additional 20 for time stamp if(log) { sprintf(cmd, "mkdir -p %s", log->path); if(system(cmd)){ fprintf(stderr, "creating of %s directory fail", log->path); } time(&log->create_time); localtime_r(&log->create_time , &cur_time); if(log->interval < 3600) { sprintf(fname,"%s/%s_%04d%02d%02d%02d%02d.log" ,log->path ,log->name ,cur_time.tm_year+1900,cur_time.tm_mon+1 ,cur_time.tm_mday ,cur_time.tm_hour ,cur_time.tm_min ); } else { sprintf(fname,"%s/%s_%04d%02d%02d%02d.log" ,log->path ,log->name ,cur_time.tm_year+1900,cur_time.tm_mon+1 ,cur_time.tm_mday ,cur_time.tm_hour ); } // if log->fp is open then close it first if(log->fp != NULL) { logger_close(log); } log->fp = fopen(fname ,"a+"); if(log->fp == NULL) { return -1; } return 0; } else { return -1; } }
//===================================== // save some stuff and free all objects void finalize(params_t *params) { // free objects gsl_rng_free(params->rng); g_array_free(params->mwheel, TRUE); // close the stats object logger_close(params->logger); // free all cells in the population int i; for (i=0; i<params->cells->len; i++) cell_free(params->cells->pdata[i]); // free cell array g_ptr_array_free(params->cells, TRUE); // free the array of daughter cells g_ptr_array_free(params->dcells, TRUE); // free the profile pool pool_free(params->pool); // ** the log_path will be freed in the end of main ** }
int main(int argc, char* argv[]) { #ifdef WIN32 acl::acl_cpp_init(); #endif logger_open("fs_benchmark.log", "fs_benchmark"); int ch; int nthreads = 2; #ifdef WIN32 snprintf(__path, sizeof(__path), "var/%d", (int) _getpid()); #else snprintf(__path, sizeof(__path), "var/%d", (int) getpid()); #endif while ((ch = getopt(argc, argv, "hn:c:l:kp:P:")) > 0) { switch (ch) { case 'h': usage(argv[0]); return 0; case 'n': nthreads = atoi(optarg); break; case 'c': __nfiles = atoi(optarg); break; case 'l': __length = atoi(optarg); break; case 'k': __kernel_event = true; break; case 'p': #ifdef WIN32 snprintf(__path, sizeof(__path), "%s/%d", optarg, (int) _getpid()); #else snprintf(__path, sizeof(__path), "%s/%d", optarg, (int) getpid()); #endif break; case 'P': __parallel = atoi(optarg); if (__parallel <= 0) __parallel = 1; break; default: break; } } for (size_t i = 0; i < sizeof(__data); i++) { __data[i] = 'X'; } acl_make_dirs(__path, 0700); // 创建线程池句柄 acl_pthread_pool_t* tp = acl_thread_pool_create(nthreads, 0); // 添加线程处理任务 for (int i = 0; i < nthreads; i++) acl_pthread_pool_add(tp, thread_main, NULL); // 销毁线程池 acl_pthread_pool_destroy(tp); logger_close(); #ifdef WIN32 printf("enter any key to exit\r\n"); getchar(); #endif return 0; }
/* * logger -- read and log utility * * Reads from an input and arranges to write the result on the system * log. */ int main(int argc, char **argv) { struct logger_ctl ctl = { .fd = -1, .pid = 0, .pri = LOG_USER | LOG_NOTICE, .prio_prefix = 0, .tag = NULL, .unix_socket = NULL, .unix_socket_errors = 0, .server = NULL, .port = NULL, .hdr = NULL, .msgid = NULL, .socket_type = ALL_TYPES, .max_message_size = 1024, .rfc5424_time = 1, .rfc5424_tq = 1, .rfc5424_host = 1, .skip_empty_lines = 0 }; int ch; int stdout_reopened = 0; int unix_socket_errors_mode = AF_UNIX_ERRORS_AUTO; #ifdef HAVE_LIBSYSTEMD FILE *jfd = NULL; #endif static const struct option longopts[] = { { "id", optional_argument, 0, OPT_ID }, { "stderr", no_argument, 0, 's' }, { "file", required_argument, 0, 'f' }, { "no-act", no_argument, 0, OPT_NOACT, }, { "priority", required_argument, 0, 'p' }, { "tag", required_argument, 0, 't' }, { "socket", required_argument, 0, 'u' }, { "socket-errors", required_argument, 0, OPT_SOCKET_ERRORS }, { "udp", no_argument, 0, 'd' }, { "tcp", no_argument, 0, 'T' }, { "server", required_argument, 0, 'n' }, { "port", required_argument, 0, 'P' }, { "version", no_argument, 0, 'V' }, { "help", no_argument, 0, 'h' }, { "octet-count", no_argument, 0, OPT_OCTET_COUNT }, { "prio-prefix", no_argument, 0, OPT_PRIO_PREFIX }, { "rfc3164", no_argument, 0, OPT_RFC3164 }, { "rfc5424", optional_argument, 0, OPT_RFC5424 }, { "size", required_argument, 0, 'S' }, { "msgid", required_argument, 0, OPT_MSGID }, { "skip-empty", no_argument, 0, 'e' }, #ifdef HAVE_LIBSYSTEMD { "journald", optional_argument, 0, OPT_JOURNALD }, #endif { NULL, 0, 0, 0 } }; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); while ((ch = getopt_long(argc, argv, "ef:ip:S:st:u:dTn:P:Vh", longopts, NULL)) != -1) { switch (ch) { case 'f': /* file to log */ if (freopen(optarg, "r", stdin) == NULL) err(EXIT_FAILURE, _("file %s"), optarg); stdout_reopened = 1; break; case 'e': ctl.skip_empty_lines = 1; break; case 'i': /* log process id also */ ctl.pid = logger_getpid(); break; case OPT_ID: if (optarg) { const char *p = optarg; if (*p == '=') p++; ctl.pid = strtoul_or_err(optarg, _("failed to parse id")); } else ctl.pid = logger_getpid(); break; case 'p': /* priority */ ctl.pri = pencode(optarg); break; case 's': /* log to standard error */ ctl.stderr_printout = 1; break; case 't': /* tag */ ctl.tag = optarg; break; case 'u': /* unix socket */ ctl.unix_socket = optarg; break; case 'S': /* max message size */ ctl.max_message_size = strtosize_or_err(optarg, _("failed to parse message size")); break; case 'd': ctl.socket_type = TYPE_UDP; break; case 'T': ctl.socket_type = TYPE_TCP; break; case 'n': ctl.server = optarg; break; case 'P': ctl.port = optarg; break; case 'V': printf(UTIL_LINUX_VERSION); exit(EXIT_SUCCESS); case 'h': usage(stdout); case OPT_OCTET_COUNT: ctl.octet_count = 1; break; case OPT_PRIO_PREFIX: ctl.prio_prefix = 1; break; case OPT_RFC3164: ctl.syslogfp = syslog_rfc3164_header; break; case OPT_RFC5424: ctl.syslogfp = syslog_rfc5424_header; if (optarg) parse_rfc5424_flags(&ctl, optarg); break; case OPT_MSGID: if (strchr(optarg, ' ')) errx(EXIT_FAILURE, _("--msgid cannot contain space")); ctl.msgid = optarg; break; #ifdef HAVE_LIBSYSTEMD case OPT_JOURNALD: if (optarg) { jfd = fopen(optarg, "r"); if (!jfd) err(EXIT_FAILURE, _("cannot open %s"), optarg); } else jfd = stdin; break; #endif case OPT_SOCKET_ERRORS: unix_socket_errors_mode = parse_unix_socket_errors_flags(optarg); break; case OPT_NOACT: ctl.noact = 1; break; case '?': default: usage(stderr); } } argc -= optind; argv += optind; if (stdout_reopened && argc) warnx(_("--file <file> and <message> are mutually exclusive, message is ignored")); #ifdef HAVE_LIBSYSTEMD if (jfd) { int ret = journald_entry(&ctl, jfd); if (stdin != jfd) fclose(jfd); if (ret) errx(EXIT_FAILURE, _("journald entry could not be written")); return EXIT_SUCCESS; } #endif switch (unix_socket_errors_mode) { case AF_UNIX_ERRORS_OFF: ctl.unix_socket_errors = 0; break; case AF_UNIX_ERRORS_ON: ctl.unix_socket_errors = 1; break; case AF_UNIX_ERRORS_AUTO: ctl.unix_socket_errors = ctl.noact || ctl.stderr_printout; #ifdef HAVE_LIBSYSTEMD ctl.unix_socket_errors |= !!sd_booted(); #endif break; default: abort(); } logger_open(&ctl); if (0 < argc) logger_command_line(&ctl, argv); else /* Note. --file <arg> reopens stdin making the below * function to be used for file inputs. */ logger_stdin(&ctl); logger_close(&ctl); return EXIT_SUCCESS; }
int main(int argc, char*argv[]) { char *conf_file_name = NULL; FILE *conf_file_pointer = NULL; char line[LINE_MAX] = {'\0'}; char *tokens[TOKEN_MAX] = {NULL}; int count = 0; gateway_create_params gateway_device = {NULL, NULL}; gateway_handle gateway = NULL; int return_value = E_FAILURE; LOG_DEBUG(("DEBUG: Number of arguments are: %d\n", argc)); if(argc<3) { LOG_ERROR(("ERROR: Please provide configuration file name\n")); return (0); } conf_file_name = argv[1]; LOG_DEBUG(("DEBUG: Configuration File Name is %s\n", conf_file_name)); conf_file_pointer = fopen(conf_file_name, "r"); if(!conf_file_pointer) { LOG_ERROR(("ERROR: Error in opening configuration file\n")); return (0); } return_value = log_open_output_file(argv[2]); if(E_SUCCESS != return_value) { LOG_ERROR(("ERROR: Unable to open the file for writing\n")); return (0); } /* Read line */ if(fgets(line, LINE_MAX, conf_file_pointer) == NULL) { LOG_DEBUG(("DEBUG: Cleanup and return\n")); fclose(conf_file_pointer); LOG_ERROR(("ERROR: Wrong configuration file\n")); return (0); } str_tokenize(line, ":\n\r", tokens, &count); if(count<2) { LOG_ERROR(("Wrong configuration file\n")); fclose(conf_file_pointer); return (0); } str_copy(&gateway_device.gateway_ip_address, tokens[0]); str_copy(&gateway_device.gateway_port_no, tokens[1]); LOG_DEBUG(("IP Address: %s\n", gateway_device.gateway_ip_address)); LOG_DEBUG(("Port No: %s\n", gateway_device.gateway_port_no)); return_value = create_gateway(&gateway, &gateway_device); if(E_SUCCESS != return_value) { LOG_ERROR(("ERROR: Unable to create gateway\n")); free(gateway_device.gateway_ip_address); free(gateway_device.gateway_port_no); fclose(conf_file_pointer); return (0); } LOG_SCREEN(("INFO: Gateway Front End started successfully\n")); LOG_SCREEN(("INFO: Output is Redirected to: %s\n", argv[2])); char choice; printf("Press any key to exit...\n"); scanf("%c", &choice); delete_gateway(gateway); free(gateway_device.gateway_ip_address); free(gateway_device.gateway_port_no); fclose(conf_file_pointer); logger_close(); return (0); }
int main(int argc, char*argv[]) { char *conf_file_name = NULL; FILE *conf_file_pointer = NULL; char line[LINE_MAX] = {'\0'}; char *tokens[TOKEN_MAX] = {NULL}; int count = 0; sensor_create_params sensor_device = {NULL, NULL, NULL}; sensor_handle sensor = NULL; LOG_DEBUG(("DEBUG: Number of arguments are: %d\n", argc)); if(argc<3) { LOG_ERROR(("ERROR: Please provide configuration file name(s)\n")); return (0); } conf_file_name = argv[1]; LOG_DEBUG(("DEBUG: Configuration File Name is %s\n", conf_file_name)); conf_file_pointer = fopen(conf_file_name, "r"); if(!conf_file_pointer) { LOG_ERROR(("ERROR: Error in opening configuration file\n")); return (0); } /* Read line */ if(fgets(line, LINE_MAX, conf_file_pointer) == NULL) { LOG_DEBUG(("DEBUG: Cleanup and return\n")); fclose(conf_file_pointer); LOG_ERROR(("ERROR: Wrong configuration file\n")); return (0); } str_tokenize(line, ":\n\r", tokens, &count); if(count<2) { LOG_ERROR(("ERROR: Wrong configuration file\n")); fclose(conf_file_pointer); return (0); } str_copy(&sensor_device.gateway_ip_address, tokens[0]); str_copy(&sensor_device.gateway_port_no, tokens[1]); LOG_DEBUG(("DEBUG: Gateway IP Address: %s\n", sensor_device.gateway_ip_address)); LOG_DEBUG(("DEBUG: Gateway Port No: %s\n", sensor_device.gateway_port_no)); /* Read line */ if (fgets(line, LINE_MAX, conf_file_pointer) == NULL) { LOG_DEBUG(("DEBUG: Cleanup and return\n")); fclose(conf_file_pointer); LOG_ERROR(("ERROR: Wrong configuration file\n")); return (0); } str_tokenize(line, ":\r\n", tokens, &count); if (count < 4) { LOG_ERROR(("ERROR: Wrong configuration file\n")); fclose(conf_file_pointer); return (0); } if(strcmp("sensor", tokens[0])!=0) { LOG_ERROR(("ERROR: Wrong configuration file\n")); fclose(conf_file_pointer); return (0); } str_copy(&sensor_device.sensor_ip_address, tokens[1]); str_copy(&sensor_device.sensor_port_no, tokens[2]); str_copy(&sensor_device.sensor_area_id, tokens[3]); sensor_device.sensor_value_file_name = argv[2]; LOG_DEBUG(("DEBUG: sensor ip_address: %s\n", sensor_device.sensor_ip_address)); LOG_DEBUG(("DEBUG: sensor port_no: %s\n", sensor_device.sensor_port_no)); LOG_DEBUG(("DEBUG: sensor area_id: %s\n", sensor_device.sensor_area_id)); int return_value = create_sensor(&sensor, &sensor_device); if(return_value != E_SUCCESS) { LOG_ERROR(("ERROR: Unable to create sensor\n")); free(sensor_device.gateway_ip_address); free(sensor_device.gateway_port_no); free(sensor_device.sensor_area_id); free(sensor_device.sensor_ip_address); free(sensor_device.sensor_port_no); fclose(conf_file_pointer); return (0); } LOG_ERROR(("Sensor started successfully\n")); char choice; printf("Press enter to exit\n"); scanf("%c", &choice); delete_sensor(sensor); free(sensor_device.gateway_ip_address); free(sensor_device.gateway_port_no); free(sensor_device.sensor_area_id); free(sensor_device.sensor_ip_address); free(sensor_device.sensor_port_no); fclose(conf_file_pointer); logger_close(); return (0); }
static int logger_rollFiles(LOG_PROPERTIES_T *pLogProperties) { int i; char buf[16]; char logNameOld[sizeof(pLogProperties->g_logName) + sizeof(buf)]; char logNameNew[sizeof(pLogProperties->g_logName) + sizeof(buf)]; size_t len; //struct timeval tv0, tv1; //int ms; #ifdef WIN32 struct _stat st; #else // WIN32 struct stat st; #endif // WIN32 //gettimeofday(&tv0, NULL); len = strlen(pLogProperties->g_logName); strcpy(logNameOld, pLogProperties->g_logName); strcpy(logNameNew, pLogProperties->g_logName); for(i = pLogProperties->g_maxFilesNum; i >= 1; i--) { if(i <= 1) { logger_close(pLogProperties); logNameOld[len] = '\0'; } else { sprintf(buf, ".%d", i-1); strcpy(&logNameOld[len], buf); } #ifdef WIN32 if(_stat(logNameOld, &st) == 0) { #else // WIN32 if(stat(logNameOld, &st) == 0) { #endif // WIN32 sprintf(buf, ".%d", i); strcpy(&logNameNew[len], buf); if(fileops_MoveFile(logNameOld, logNameNew) != 0) { // call internal logger_log method to avoid infinite loop possibility by // checking for roll logger_log(pLogProperties, X_ERROR("failed to move file '%s' -> '%s'"), logNameOld, logNameNew); } } } logger_open(pLogProperties); //gettimeofday(&tv1, NULL); //ms = ((tv1.tv_sec - tv0.tv_sec) * 1000) + ((tv1.tv_usec - tv0.tv_usec) / 1000); // call internal logger_log method to avoid infinite loop possibility by // checking for roll //logger_log(pLogProperties, X_DEBUG("logger_rollFiles took %d ms to complete."), ms); return 0; } extern void avc_dumpHex(void *fparg, const unsigned char *buf, unsigned int len, int ascii); void logger_LogHex(int sev, const void *buf, unsigned int len, int printAscii) { LOG_PROPERTIES_T *pLogProperties = NULL; pLogProperties = g_plogProps; if(!((pLogProperties->g_log_flags & (LOG_FLAG_USESYSLOG | LOG_FLAG_USESTDERR)) || (pLogProperties->g_fp != NULL && (pLogProperties->g_log_flags & LOG_FLAG_USEFILEOUTPUT)))) { return; } if(pLogProperties->g_log_flags & LOG_FLAG_USELOCKING) { pthread_mutex_lock(&pLogProperties->g_mtx_log); } if((pLogProperties->g_log_flags & LOG_FLAG_USESTDERR) && ((sev > 0 && sev <= pLogProperties->g_log_level_stderr) || (sev < 0 && sev >= (-1 * pLogProperties->g_log_level_stderr)))) { avc_dumpHex(stderr, (const unsigned char *) buf, len, printAscii); } if(pLogProperties->g_fp && (pLogProperties->g_log_flags & LOG_FLAG_USEFILEOUTPUT) && ((sev > 0 && sev <= pLogProperties->g_log_level) || (sev < 0 && sev >= (-1 * pLogProperties->g_log_level)))) { avc_dumpHex(pLogProperties->g_fp, (const unsigned char *) buf, len, printAscii); if(logger_checkRoll(pLogProperties) > 0) { logger_rollFiles(pLogProperties); } } if(pLogProperties->g_log_flags & LOG_FLAG_USELOCKING) { pthread_mutex_unlock(&pLogProperties->g_mtx_log); } }
int logger_SetFile(const char *fileDir, const char *fileName, unsigned int maxFilesNum, unsigned int maxFileSz, unsigned int outputFlags) { size_t len = 0u, len2 = 0u; LOG_PROPERTIES_T *pLogProperties = g_plogProps; pthread_mutex_init(&pLogProperties->g_mtx_log, NULL); if(pLogProperties->g_fp) { logger_close(pLogProperties); } if(!fileName) { return -1; } // // Init of "global" variables // pLogProperties->g_maxFilesNum = maxFilesNum; pLogProperties->g_maxFileSz = maxFileSz; // XOR with flags suitable for fileoutput pLogProperties->g_log_flags |= LOG_FLAG_USEFILEOUTPUT | (outputFlags & (LOG_FLAG_USELOCKING | LOG_OUTPUT_PRINT_DATE | LOG_OUTPUT_PRINT_SEV | LOG_OUTPUT_PRINT_SEV_ERROR | LOG_OUTPUT_PRINT_PID | LOG_OUTPUT_PRINT_TID | LOG_OUTPUT_PRINT_TAG)); if(fileDir) { len = strlen(fileDir); } len2 = strlen(fileName); if(len + len2 + 6 >= sizeof(pLogProperties->g_logName)) { return -1; } memset(pLogProperties->g_logName, 0, sizeof(pLogProperties->g_logName)); if(fileDir && len > 0) { strncpy(pLogProperties->g_logName, fileDir, sizeof(pLogProperties->g_logName) -1); if(pLogProperties->g_logName[len - 1] != DIR_DELIMETER) { pLogProperties->g_logName[len++] = DIR_DELIMETER; } } strncpy(&pLogProperties->g_logName[len], fileName, sizeof(pLogProperties->g_logName) - (len +1)); len += len2; if(!strstr(fileName, ".log")) { strncat(&pLogProperties->g_logName[len], ".log", sizeof(pLogProperties->g_logName) - (len+1)); } return logger_open(pLogProperties); }
/* * logger -- read and log utility * * Reads from an input and arranges to write the result on the system * log. */ int main(int argc, char **argv) { struct logger_ctl ctl = { .fd = -1, .pid = 0, .pri = LOG_NOTICE, .prio_prefix = 0, .tag = NULL, .unix_socket = NULL, .server = NULL, .port = NULL, .socket_type = ALL_TYPES, .rfc5424_time = 1, .rfc5424_tq = 1, .rfc5424_host = 1, }; int ch; int stdout_reopened = 0; #ifdef HAVE_LIBSYSTEMD FILE *jfd = NULL; #endif static const struct option longopts[] = { { "id", optional_argument, 0, 'i' }, { "stderr", no_argument, 0, 's' }, { "file", required_argument, 0, 'f' }, { "priority", required_argument, 0, 'p' }, { "tag", required_argument, 0, 't' }, { "socket", required_argument, 0, 'u' }, { "udp", no_argument, 0, 'd' }, { "tcp", no_argument, 0, 'T' }, { "server", required_argument, 0, 'n' }, { "port", required_argument, 0, 'P' }, { "version", no_argument, 0, 'V' }, { "help", no_argument, 0, 'h' }, { "prio-prefix", no_argument, 0, OPT_PRIO_PREFIX }, { "rfc3164", no_argument, 0, OPT_RFC3164 }, { "rfc5424", optional_argument, 0, OPT_RFC5424 }, #ifdef HAVE_LIBSYSTEMD { "journald", optional_argument, 0, OPT_JOURNALD }, #endif { NULL, 0, 0, 0 } }; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); while ((ch = getopt_long(argc, argv, "f:i::p:st:u:dTn:P:Vh", longopts, NULL)) != -1) { switch (ch) { case 'f': /* file to log */ if (freopen(optarg, "r", stdin) == NULL) err(EXIT_FAILURE, _("file %s"), optarg); stdout_reopened = 1; break; case 'i': /* log process id also */ if (optarg) { const char *p = optarg; if (*p == '=') p++; ctl.pid = strtoul_or_err(optarg, _("failed to parse id")); } else ctl.pid = getpid(); break; case 'p': /* priority */ ctl.pri = pencode(optarg); break; case 's': /* log to standard error */ ctl.stderr_printout = 1; break; case 't': /* tag */ ctl.tag = optarg; break; case 'u': /* unix socket */ ctl.unix_socket = optarg; break; case 'd': ctl.socket_type = TYPE_UDP; break; case 'T': ctl.socket_type = TYPE_TCP; break; case 'n': ctl.server = optarg; break; case 'P': ctl.port = optarg; break; case 'V': printf(UTIL_LINUX_VERSION); exit(EXIT_SUCCESS); case 'h': usage(stdout); case OPT_PRIO_PREFIX: ctl.prio_prefix = 1; break; case OPT_RFC3164: ctl.syslogfp = syslog_rfc3164; break; case OPT_RFC5424: ctl.syslogfp = syslog_rfc5424; if (optarg) parse_rfc5424_flags(&ctl, optarg); break; #ifdef HAVE_LIBSYSTEMD case OPT_JOURNALD: if (optarg) { jfd = fopen(optarg, "r"); if (!jfd) err(EXIT_FAILURE, _("cannot open %s"), optarg); } else jfd = stdin; break; #endif case '?': default: usage(stderr); } } argc -= optind; argv += optind; if (stdout_reopened && argc) warnx(_("--file <file> and <message> are mutually exclusive, message is ignored")); #ifdef HAVE_LIBSYSTEMD if (jfd) { int ret = journald_entry(jfd); if (stdin != jfd) fclose(jfd); if (ret) errx(EXIT_FAILURE, _("journald entry could not be wrote")); return EXIT_SUCCESS; } #endif logger_open(&ctl); if (0 < argc) logger_command_line(&ctl, argv); else /* Note. --file <arg> reopens stdin making the below * function to be used for file inputs. */ logger_stdin(&ctl); logger_close(&ctl); return EXIT_SUCCESS; }
int main(int argc, char *argv[]) { int ret = EX_OK; primwatch_t primwatch; const char *log_type; const char *log_facility; const char *log_prefix; char log_path[MAXPATHLEN]; const char *pid_file_path; const char *cntrl_addr; const char *cntrl_port; int64_t verbose_level; if (logger_create()) { fprintf(stderr, "failed in create logger"); ret = EX_OSERR; } if (primwatch_initialize(&primwatch)) { fprintf(stderr, "failed in initaizliae"); ret = EX_OSERR; goto last; } parse_args(&primwatch, argc, argv); if (logger_set_foreground(primwatch.foreground)) { fprintf(stderr, "failed in create logger"); ret = EX_OSERR; } if (config_manager_load(primwatch.config_manager, primwatch.config_file)) { LOG(LOG_LV_ERR, "failed in load config file %s", primwatch.config_file); ret = EX_DATAERR; goto last; } if (config_manager_get_string(primwatch.config_manager, &log_type , "logType", NULL)) { LOG(LOG_LV_ERR, "failed in get log type from config"); ret = EX_DATAERR; goto last; } if (config_manager_get_string(primwatch.config_manager, &log_facility , "logFacility", NULL)) { LOG(LOG_LV_ERR, "failed in get log facility from config"); ret = EX_DATAERR; goto last; } if (config_manager_get_string(primwatch.config_manager, &log_prefix , "logPath", NULL)) { LOG(LOG_LV_ERR, "failed in get log path from config"); ret = EX_DATAERR; goto last; } if (config_manager_get_string(primwatch.config_manager, &pid_file_path , "pidFilePath", NULL)) { LOG(LOG_LV_ERR, "failed in get pid file path from config"); ret = EX_DATAERR; goto last; } if (config_manager_get_string(primwatch.config_manager, &cntrl_addr , "controllerAddress", NULL)) { LOG(LOG_LV_ERR, "failed in get controller address from config"); ret = EX_DATAERR; goto last; } if (config_manager_get_string(primwatch.config_manager, &cntrl_port , "controllerPort", NULL)) { LOG(LOG_LV_ERR, "failed in get controller port from config"); ret = EX_DATAERR; goto last; } if (config_manager_get_long(primwatch.config_manager, &verbose_level , "verboseLevel", NULL)) { LOG(LOG_LV_ERR, "failed in get verbose level from config"); ret = EX_DATAERR; goto last; } snprintf(log_path, sizeof(log_path), "%s.daemon", log_prefix); if (logger_open((log_level_t)verbose_level, log_type, PROGIDENT, LOG_PID, log_facility, log_path)) { LOG(LOG_LV_ERR, "failed in open log"); ret = EX_OSERR; goto last; } if (!primwatch.foreground) { if (daemon(1,1)) { LOG(LOG_LV_ERR, "failed in daemon"); ret = EX_OSERR; goto last; } setsid(); } if (primwatch_event_initialize(&primwatch)) { fprintf(stderr, "failed in initaizliae"); ret = EX_OSERR; goto last; } if (make_pidfile(pid_file_path)) { LOG(LOG_LV_ERR, "failed in create file of process id"); ret = EX_OSERR; goto last; } if (watcher_polling_start(primwatch.watcher)) { LOG(LOG_LV_ERR, "failed in initial polling"); ret = EX_OSERR; goto last; } if (controller_start(primwatch.controller, cntrl_addr, cntrl_port)) { LOG(LOG_LV_ERR, "failed in start controller"); ret = EX_OSERR; goto last; } signal_set(&primwatch.sig_int_event, SIGINT, primwatch_terminate, &primwatch); event_priority_set(&primwatch.sig_int_event, DEFAULT_EVENT_PRIORITY + 30); event_base_set(primwatch.event_base, &primwatch.sig_int_event); signal_add(&primwatch.sig_int_event, NULL); signal_set(&primwatch.sig_term_event, SIGTERM, primwatch_terminate, &primwatch); event_priority_set(&primwatch.sig_term_event, DEFAULT_EVENT_PRIORITY + 30); event_base_set(primwatch.event_base, &primwatch.sig_term_event); signal_add(&primwatch.sig_term_event, NULL); signal_set(&primwatch.sig_hup_event, SIGHUP, primwatch_reload, &primwatch); event_priority_set(&primwatch.sig_hup_event, DEFAULT_EVENT_PRIORITY + 30); event_base_set(primwatch.event_base, &primwatch.sig_hup_event); signal_add(&primwatch.sig_hup_event, NULL); signal_set(&primwatch.sig_chld_event, SIGCHLD, primwatch_sigchild, &primwatch); event_priority_set(&primwatch.sig_chld_event, DEFAULT_EVENT_PRIORITY + 30); event_base_set(primwatch.event_base, &primwatch.sig_chld_event); signal_add(&primwatch.sig_chld_event, NULL); if (event_base_dispatch(primwatch.event_base) == -1) { LOG(LOG_LV_ERR, "failed in event base dispatch"); ret = EX_OSERR; goto last; } last: unlink(pid_file_path); logger_close(); logger_destroy(); primwatch_finalize(&primwatch); return ret; }
int main(int argc, char*argv[]) { char *conf_file_name = NULL; FILE *conf_file_pointer = NULL; char line[LINE_MAX] = {'\0'}; char *tokens[TOKEN_MAX] = {NULL}; int count = 0; gateway_create_params gateway_device = {NULL, NULL}; gateway_handle gateway = NULL; int return_value = E_FAILURE; LOG_DEBUG(("DEBUG: Number of arguments are: %d\n", argc)); if(argc<2) { LOG_ERROR(("ERROR: Please provide configuration file name\n")); return (0); } conf_file_name = argv[1]; LOG_DEBUG(("DEBUG: Configuration File Name is %s\n", conf_file_name)); conf_file_pointer = fopen(conf_file_name, "r"); if(!conf_file_pointer) { LOG_ERROR(("ERROR: Error in opening configuration file\n")); return (0); } /* Read line */ if(fgets(line, LINE_MAX, conf_file_pointer) == NULL) { LOG_DEBUG(("DEBUG: Cleanup and return\n")); fclose(conf_file_pointer); LOG_ERROR(("ERROR: Wrong configuration file\n")); return (0); } str_tokenize(line, ":\n\r", tokens, &count); if(count<2) { LOG_ERROR(("Wrong configuration file\n")); fclose(conf_file_pointer); return (0); } str_copy(&gateway_device.gateway_ip_address, tokens[0]); str_copy(&gateway_device.gateway_port_no, tokens[1]); LOG_DEBUG(("IP Address: %s\n", gateway_device.gateway_ip_address)); LOG_DEBUG(("Port No: %s\n", gateway_device.gateway_port_no)); LOG_GATEWAY(("------------------------------------------------\n")); return_value = create_gateway(&gateway, &gateway_device); if(E_SUCCESS != return_value) { LOG_ERROR(("ERROR: Unable to create gateway\n")); free(gateway_device.gateway_ip_address); free(gateway_device.gateway_port_no); fclose(conf_file_pointer); return (0); } LOG_ERROR(("Gateway started successfully\n")); int choice, interval; while(1) { printf("-------------------Menu----------------\n"); printf("1.Change Sensor interval\n"); printf("2.Exit\n"); printf("Enter your choice: "); scanf("%d", &choice); if(choice==2) break; switch(choice) { case 1: print_sensors(gateway); printf("Enter sensor id: "); scanf("%d", &choice); printf("Enter new interval: "); scanf("%d", &interval); return_value = set_interval(gateway, choice, interval); if(E_SUCCESS != return_value) { printf("Unable to set the interval\n"); } break; default: printf("Enter valid choice...\n"); } } delete_gateway(gateway); LOG_GATEWAY(("-------------------------------------------------\n")); free(gateway_device.gateway_ip_address); free(gateway_device.gateway_port_no); fclose(conf_file_pointer); logger_close(); return (0); }