54 #if defined OS_LINUX || defined OS_CYGWIN
56 #elif defined OS_MACOS || defined OS_FREEBSD || defined OS_OPENBSD
64 #include "co_string.h"
66 #include "rt_errh_msg.h"
67 #include "rt_pwr_msg.h"
69 #define UNKNOWN_PROGRAM_NAME "Unknown name "
73 typedef enum { eArg_sts, eArg_int, eArg_string } eArg;
84 #if defined OS_LINUX || defined OS_CYGWIN
87 static mqd_t mqid = (mqd_t)-1;
88 static unsigned int prio = 0;
89 static int mq_send_errno = 0;
90 #elif defined OS_MACOS || defined OS_FREEBSD || defined OS_OPENBSD
95 static int mq_send_errno = 0;
98 static char programName[16];
99 static int interactive = 0;
100 static int initDone = 0;
103 static char* get_header(
char,
char*);
104 static char* get_message(
const int,
unsigned int,
char*,
int);
105 static char* get_name(
char*,
int);
107 static void openLog();
108 static void set_name(
const char*);
110 static void log_message(errh_sLog*,
char,
const char*, va_list);
113 static char anix_name[40][32] = {
141 "rt_websocketserver",
146 void errh_AnixName(
errh_eAnix anix,
char *name)
148 if (anix > 0 && anix <= 40)
149 strcpy(name, anix_name[anix - 1]);
154 void errh_Interactive(
void)
173 get_name(programName,
sizeof(programName) - 1);
174 if (name != NULL && name[0] !=
'\0')
229 char* errh_GetMsg(
const int sts,
char* buf,
int bufSize)
231 return get_message(sts, 0xf, buf, bufSize);
237 char* errh_GetText(
const int sts,
char* buf,
int bufSize)
239 return get_message(sts, 1, buf, bufSize);
243 char* errh_Log(
char* buff,
char severity,
const char* msg, ...)
248 s = get_header(severity, buff);
250 msg_vsprintf(s, msg, NULL, ap);
254 printf(
"%s\n", buff);
278 log_message(NULL,
'S', msg, args);
299 log_message(NULL,
'I', msg, args);
320 log_message(NULL,
'W', msg, args);
341 log_message(NULL,
'E', msg, args);
362 log_message(NULL,
'F', msg, args);
367 void errh_LogFatal(errh_sLog* lp,
const char* msg, ...)
372 log_message(lp,
'F', msg, args);
377 void errh_LogWarning(errh_sLog* lp,
const char* msg, ...)
382 log_message(lp,
'W', msg, args);
387 void errh_LogError(errh_sLog* lp,
const char* msg, ...)
392 log_message(lp,
'E', msg, args);
397 void errh_LogInfo(errh_sLog* lp,
const char* msg, ...)
402 log_message(lp,
'I', msg, args);
407 void errh_LogSuccess(errh_sLog* lp,
const char* msg, ...)
412 log_message(lp,
'S', msg, args);
422 eap = (sArg*)malloc(
sizeof(*eap));
423 eap->class = eArg_sts;
431 void* errh_ErrArgAF(
char* str)
435 eap = (sArg*)malloc(
sizeof(*eap));
437 eap->class = eArg_string;
445 void* errh_ErrArgL(
int val)
449 eap = (sArg*)malloc(
sizeof(*eap));
451 eap->class = eArg_int;
467 get_message(sts, 0xb, msg,
sizeof(msg));
470 while ((eap = va_arg(ap, sArg*)) != NULL) {
471 switch (eap->class) {
473 args[argno++] = (
char*)((
long int)eap->u.sts);
477 args[argno++] = (
char*)((
long int)eap->u.intval);
480 args[argno++] = eap->u.string;
490 memset(&tmp, 0,
sizeof(tmp));
491 s = get_header(get_severity(sts),
string);
492 msg_vsprintf(s, msg, args, tmp);
498 char* errh_Message(
char*
string,
char severity,
char* msg, ...)
503 s = get_header(severity,
string);
505 msg_vsprintf(s, msg, NULL, ap);
524 static char* get_message(
525 const pwr_tStatus sts,
unsigned int flags,
char* buf,
int bufSize)
527 return msg_GetMessage(sts, flags, buf, bufSize);
530 static void set_name(
const char* name)
532 strncpy(programName, name,
sizeof(programName) - 1);
533 programName[
sizeof(programName) - 1] =
'\0';
536 static void openLog()
538 #if defined OS_LINUX || defined OS_CYGWIN
539 if (mqid == (mqd_t)-1) {
541 char* busid = getenv(pwr_dEnvBusId);
543 sprintf(name,
"%s_%s", LOG_QUEUE_NAME, busid ? busid :
"");
544 mqid = mq_open(name, O_WRONLY | O_NONBLOCK, 0, 0);
545 if (mqid == (mqd_t)-1) {
550 string,
'E',
"Open messageQ, mq_open(%s)\n%s", name, strerror(errno));
555 #elif defined OS_MACOS || defined OS_FREEBSD || defined OS_OPENBSD
556 if (mqid == (key_t)-1) {
558 char* busid = getenv(pwr_dEnvBusId);
561 int flags = O_RDWR | O_CREAT;
562 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
564 sprintf(name,
"%s_%s", LOG_QUEUE_NAME, busid ? busid :
"");
565 fd = open(name, flags, mode);
567 printf(
"Message Queue, open failed on %s, errno: %d\n", name, errno);
569 key = ftok(name,
'm');
572 mqid = msgget(key, IPC_CREAT | 0660);
577 s = errh_Message(
string,
'E',
"Open message queue, msgget(%s)\n%s", name,
586 static char* get_name(
char* name,
int size)
588 int len = strlen(UNKNOWN_PROGRAM_NAME);
590 strncpy(name, UNKNOWN_PROGRAM_NAME, MIN(size, len));
592 name[MIN(size, len)] =
'\0';
596 static sPid* get_pid(sPid* pid)
603 static char* get_header(
char severity,
char* s)
613 s += sprintf(s,
"%c ", severity);
621 s += sprintf(s,
"%c %-*.*s", severity, (
int)
sizeof(programName),
622 (
int)
sizeof(programName), programName);
624 time_t sec = time.tv_sec;
625 localtime_r(&sec, &tp);
627 s += sprintf(s,
" %8d ", pid);
629 s += sprintf(s,
"%02d-%02d-%02d %02d:%02d:%02d.%02d ", t->tm_year % 100,
630 t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec,
631 (
int)(time.tv_nsec / 10000000));
638 static void log_message(
639 errh_sLog* lp,
char severity,
const char* msg, va_list ap)
644 s = get_header(severity,
string);
645 msg_vsprintf(s, msg, NULL, ap);
647 printf(
"%s\n",
string);
651 if (lp != NULL && lp->send) {
652 lp->put.data = string;
653 lp->put.size = strlen(
string) + 1;
654 lp->put.allocate = 1;
655 qcom_Put(NULL, &lp->logQ, &lp->put);
683 static void errh_send(
686 #if defined OS_LINUX || defined OS_CYGWIN
689 if (mqid != (mqd_t)-1) {
692 switch (message_type) {
694 strncpy(msg.str, s, LOG_MAX_MSG_SIZE);
695 str_Cut(msg.str, LOG_MAX_MSG_SIZE);
696 msg.message_type = message_type;
697 msg.severity = severity;
699 msg.anix = errh_anix;
700 len =
sizeof(msg) -
sizeof(msg.message_type) -
sizeof(msg.str)
701 + strlen(msg.str) + 1;
704 msg.message_type = message_type;
706 msg.anix = errh_anix;
707 len =
sizeof(msg) -
sizeof(msg.message_type) -
sizeof(msg.str);
711 prio = sysconf(_SC_MQ_PRIO_MAX) - 1;
712 if (mq_send(mqid, (
char*)&msg, MIN(len, LOG_MAX_MSG_SIZE - 1), prio)
714 if (mq_send_errno != errno) {
715 mq_send_errno = errno;
724 #elif defined OS_MACOS || defined OS_FREEBSD || defined OS_OPENBSD
730 switch (message_type) {
732 strncpy(msg.str, s, LOG_MAX_MSG_SIZE);
733 str_Cut(msg.str, LOG_MAX_MSG_SIZE);
734 msg.message_type = message_type;
735 msg.severity = severity;
737 msg.anix = errh_anix;
738 len =
sizeof(msg) -
sizeof(msg.message_type) -
sizeof(msg.str)
739 + strlen(msg.str) + 1;
742 msg.message_type = message_type;
744 msg.anix = errh_anix;
745 len =
sizeof(msg) -
sizeof(msg.message_type) -
sizeof(msg.str);
750 if (msgsnd(mqid, (
char*)&msg, MIN(len, LOG_MAX_MSG_SIZE - 1), 0) == -1) {
751 if (mq_send_errno != errno) {
752 mq_send_errno = errno;
774 return errh_eSeverity_Null;
778 return errh_eSeverity_Success;
780 return errh_eSeverity_Info;
782 return errh_eSeverity_Warning;
784 return errh_eSeverity_Error;
786 return errh_eSeverity_Fatal;
788 return errh_eSeverity_Null;
Include file for Time management.
void errh_Error(const char *msg,...)
Log an error message. The function has a variable argument list similar to sprintf.
pwr_tStatus errh_Init(const char *name, errh_eAnix anix)
Initialize errh. Set up a queue to write to, set application name.
void errh_SetStatus(pwr_tStatus sts)
Set application status. The application status is showed in the Node graph. The application can set t...
errh_eAnix errh_Anix(void)
Get application index for the process.
void errh_Success(const char *msg,...)
Log a success message. The function has a variable argument list similar to sprintf.
void errh_SetName(char *name)
Set application name for the process.
void errh_Fatal(const char *msg,...)
Log a fatal message. The function has a variable argument list similar to sprintf.
void errh_SetAnix(errh_eAnix anix)
Set application index for the process.
void errh_Info(const char *msg,...)
Log an info message. The function has a variable argument list similar to sprintf.
void errh_Warning(const char *msg,...)
Log a warning message. The function has a variable argument list similar to sprintf.
pwr_tBoolean qcom_Put(pwr_tStatus *sts, const qcom_sQid *receiver, qcom_sPut *put)
Put a new message.
int time_GetTime(pwr_tTime *ts)
Get current time.
int pwr_tStatus
Status type.
Include file for Gdh Global Data Handler.