Last active
December 14, 2021 09:32
-
-
Save sutyum/281c27f7c2d28c7ea3d740f2ccc95cd5 to your computer and use it in GitHub Desktop.
Logging to Linux Syslog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <pthread.h> | |
#include <sched.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <sys/syslog.h> | |
#define THREAD_COUNT 128 | |
static const char *g_log_header = "[MYLOGS]"; | |
typedef struct { | |
int threadIdx; | |
} threadParams_t; | |
// POSIX thread declarations and scheduling attributes | |
// | |
static pthread_t threads[THREAD_COUNT]; | |
static threadParams_t threadParams[THREAD_COUNT]; | |
// Thread | |
// | |
void *incThread(void *threadp) { | |
int i; | |
threadParams_t *threadParams = (threadParams_t *)threadp; | |
int gsum = 0; | |
for (i = 1; i <= threadParams->threadIdx; i++) { | |
gsum = gsum + i; | |
} | |
syslog(LOG_INFO, "Thread idx=%d, sum[1...%d]=%d\n", threadParams->threadIdx, | |
threadParams->threadIdx, gsum); | |
} | |
int clear_syslog() { | |
if (system("truncate -s 0 /var/log/syslog")) | |
return -1; | |
return 0; | |
} | |
int main(int argc, char *argv[]) { | |
if (clear_syslog()) { | |
printf("Warning: Failed to clear log!"); | |
} | |
openlog(g_log_header, LOG_NDELAY, LOG_DAEMON); | |
char result[1024] = {0x0}; | |
sprintf(result, R"(logger "%s: `uname -a`")", g_log_header); | |
if (system(result)) { | |
printf("Error to print hardware information to the syslog.\n"); | |
} | |
for (int i = 0; i < THREAD_COUNT; i++) { | |
threadParams[i].threadIdx = i; | |
pthread_create(&threads[i], // pointer to thread descriptor | |
(void *)0, // use default attributes | |
incThread, // thread function entry point | |
(void *)&(threadParams[i]) // parameters to pass in | |
); | |
} | |
for (int i = 0; i < THREAD_COUNT; i++) | |
pthread_join(threads[i], NULL); | |
printf("TEST COMPLETE\n"); | |
closelog(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment