📄 loglib.c
字号:
* EXAMPLE* If the following code were executed by task 20:* .CS* {* name = "GRONK";* num = 123;** logMsg ("ERROR - name = %s, num = %d.\en", name, num, 0, 0, 0, 0);* }* .CE* the following error message would appear on the system log:* .CS* 0x180400 (t20): ERROR - name = GRONK, num = 123.* .CE** RETURNS: The number of bytes written to the log queue,* or EOF if the routine is unable to write a message.** SEE ALSO: printf(), logTask()*/int logMsg ( char *fmt, /* format string for print */ int arg1, /* first of six required args for fmt */ int arg2, int arg3, int arg4, int arg5, int arg6 ) { int timeout; LOG_MSG msg; if (INT_CONTEXT ()) { msg.id = -1; timeout = NO_WAIT; } else { msg.id = taskIdSelf (); timeout = WAIT_FOREVER; } msg.fmt = fmt; msg.arg[0] = arg1; msg.arg[1] = arg2; msg.arg[2] = arg3; msg.arg[3] = arg4; msg.arg[4] = arg5; msg.arg[5] = arg6; if (msgQSend (logMsgQId, (char *) &msg, sizeof (msg), timeout, MSG_PRI_NORMAL) != OK) { ++logMsgsLost; return (EOF); } return (sizeof (msg)); }/********************************************************************************* logFdSet - set the primary logging file descriptor** This routine changes the file descriptor where messages from logMsg() * are written, allowing the log device to be changed from the default* specified by logInit(). It first removes the old file descriptor (if * one had been previously set) from the log file descriptor list, then * adds the new <fd>.** The old logging file descriptor is not closed or affected by this call; * it is simply no longer used by the logging facilities.** RETURNS: N/A** SEE ALSO: logFdAdd(), logFdDelete()*/void logFdSet ( int fd /* file descriptor to use as logging device */ ) { static int oldLogFd = NONE; if (oldLogFd != NONE) logFdDelete (oldLogFd); /* if we are called from an rlogin session, remove pty fd from log fd list */ if (logFdFromRlogin != NONE) { logFdDelete (logFdFromRlogin); logFdFromRlogin = NONE; /* reset since its deleted from list */ } if (logFdAdd (fd) == OK) oldLogFd = fd; else oldLogFd = NONE; }/********************************************************************************* logFdAdd - add a logging file descriptor** This routine adds to the log file descriptor list another file descriptor* <fd> to which messages will be logged. The file descriptor must be a * valid open file descriptor.** RETURNS:* OK, or ERROR if the allowable number of additional logging file descriptors* (5) is exceeded.** SEE ALSO: logFdDelete()*/STATUS logFdAdd ( int fd /* file descriptor for additional logging device */ ) { semTake (&logFdSem, WAIT_FOREVER); if ((numLogFds + 1) > MAX_LOGFDS) { semGive (&logFdSem); /* XXX errnoSet (S_logLib_TOO_MANY_LOGGING_FDS); */ return (ERROR); } logFd [numLogFds++] = fd; semGive (&logFdSem); return (OK); }/********************************************************************************* logFdDelete - delete a logging file descriptor** This routine removes from the log file descriptor list a logging file * descriptor added by logFdAdd(). The file descriptor is not closed; but is* no longer used by the logging facilities.** RETURNS:* OK, or ERROR if the file descriptor was not added with logFdAdd().** SEE ALSO: logFdAdd()*/STATUS logFdDelete ( int fd /* file descriptor to stop using as logging device */ ) { FAST int ix; FAST int jx; semTake (&logFdSem, WAIT_FOREVER); for (ix = jx = 0; ix < numLogFds; ix++, jx++) { /* shift array of logFd's after deleting unwanted fd */ if (((logFd [jx] = logFd [ix]) == fd) && ix == jx) jx--; } if (ix == jx) { semGive (&logFdSem); return (ERROR); /* didn't find specified fd */ } numLogFds--; semGive (&logFdSem); return (OK); }/********************************************************************************* logTask - message-logging support task** This routine prints the messages logged with logMsg(). It waits on a* message queue and prints the messages as they arrive on the file descriptor* specified by logInit() (or a subsequent call to logFdSet() or logFdAdd()).** This task is spawned by logInit().** RETURNS: N/A** SEE ALSO: logMsg()*/void logTask (void) { static int oldMsgsLost; int newMsgsLost; /* used in case logMsgsLost is changed during use */ LOG_MSG msg; char *checkName; FOREVER { if (msgQReceive (logMsgQId, (char *) &msg, sizeof (msg), WAIT_FOREVER) != sizeof (msg)) lprintf ("logTask: error reading log messages.\n", 0, 0, 0, 0, 0,0); else { /* print task ID followed by caller's message */ /* print task ID */ if (msg.id == -1) { lprintf ("interrupt: ", 0, 0, 0, 0, 0, 0); } else { if ((checkName = taskName (msg.id)) == NULL) lprintf ("%#x (): task dead", msg.id, 0, 0, 0, 0, 0); else lprintf ("%#x (%s): ", msg.id, (int)checkName, 0, 0, 0, 0); } if (msg.fmt == NULL) lprintf ("<null \"fmt\" parameter>\n", 0, 0, 0, 0, 0, 0); else { lprintf (msg.fmt, msg.arg[0], msg.arg[1], msg.arg[2], msg.arg[3], msg.arg[4], msg.arg[5]); } } /* check for any more messages lost */ newMsgsLost = logMsgsLost; if (newMsgsLost != oldMsgsLost) { lprintf ("logTask: %d log messages lost.\n", newMsgsLost - oldMsgsLost, 0, 0, 0, 0, 0); oldMsgsLost = newMsgsLost; } } }/********************************************************************************* lprintf - log printf** Performs an fdprintf on all logFds.*/LOCAL void lprintf ( char *fmt, /* format string for print */ int arg1, /* optional arguments to fmt */ int arg2, int arg3, int arg4, int arg5, int arg6 ) { FAST int ix; semTake (&logFdSem, WAIT_FOREVER); for (ix = 0; ix < numLogFds; ix++) fdprintf (logFd [ix], fmt, arg1, arg2, arg3, arg4, arg5, arg6); semGive (&logFdSem); }/********************************************************************************* logShow - show active logging fd's (debug only)** NOMANUAL*/void logShow (void) { FAST int ix; printf ("%3s %3s\n", "num", "fd"); printf ("%3s %3s\n", "---", "--"); for (ix = 0; ix < numLogFds; ix++) printf ("%3d %3d\n", ix, logFd [ix]); /* XXX timeout, message size, ... msgQShow (logMsgQId, 1); */ }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -