print thread name in messages even if time stamps are not printed

This commit is contained in:
2021-07-01 15:30:09 +02:00
parent 6b05e006da
commit f495dd9853
3 changed files with 27 additions and 7 deletions

View File

@ -333,7 +333,6 @@ epicsExportAddress(drvet, stream);
#ifdef EPICS_3_13
void streamEpicsPrintTimestamp(char* buffer, size_t size)
{
size_t tlen;
char* c;
TS_STAMP tm;
tsLocalTime (&tm);
@ -342,16 +341,17 @@ void streamEpicsPrintTimestamp(char* buffer, size_t size)
if (c) {
c[4] = 0;
}
tlen = strlen(buffer);
sprintf(buffer+tlen, " %.*s", (int)(size-tlen-2), taskName(0));
}
static const char* epicsThreadGetNameSelf()
{
return taskName(0);
}
#else // !EPICS_3_13
void streamEpicsPrintTimestamp(char* buffer, size_t size)
{
size_t tlen;
epicsTime tm = epicsTime::getCurrent();
tlen = tm.strftime(buffer, size, "%Y/%m/%d %H:%M:%S.%06f");
sprintf(buffer+tlen, " %.*s", (int)(size-tlen-2), epicsThreadGetNameSelf());
tm.strftime(buffer, size, "%Y/%m/%d %H:%M:%S.%06f");
}
#endif // !EPICS_3_13
@ -471,6 +471,7 @@ drvInit()
debug("StreamProtocolParser::path = %s\n",
StreamProtocolParser::path);
StreamPrintTimestampFunction = streamEpicsPrintTimestamp;
StreamGetThreadNameFunction = epicsThreadGetNameSelf;
#ifdef WITH_IOC_RUN
initHookRegister(initHook);

View File

@ -97,6 +97,7 @@ static void printTimestamp(char* buffer, size_t size)
}
void (*StreamPrintTimestampFunction)(char* buffer, size_t size) = printTimestamp;
const char* (*StreamGetThreadNameFunction)(void) = NULL;
void StreamError(const char* fmt, ...)
{
@ -116,13 +117,18 @@ void StreamError(int line, const char* file, const char* fmt, ...)
void StreamVError(int line, const char* file, const char* fmt, va_list args)
{
char timestamp[40];
if (!(streamError || streamDebug)) return; // Error logging disabled
char timestamp[40];
const char *threadname = NULL;
int timeStamped = streamMsgTimeStamped;
if (timeStamped)
{
StreamPrintTimestampFunction(timestamp, sizeof(timestamp));
}
if (StreamGetThreadNameFunction)
{
threadname = StreamGetThreadNameFunction();
}
#ifdef va_copy
if (StreamDebugFile)
{
@ -132,6 +138,10 @@ void StreamVError(int line, const char* file, const char* fmt, va_list args)
{
fprintf(StreamDebugFile, "%s ", timestamp);
}
if (threadname)
{
fprintf(StreamDebugFile, "%s ", threadname);
}
vfprintf(StreamDebugFile, fmt, args2);
fflush(StreamDebugFile);
va_end(args2);
@ -142,6 +152,10 @@ void StreamVError(int line, const char* file, const char* fmt, va_list args)
{
fprintf(stderr, "%s ", timestamp);
}
if (threadname)
{
fprintf(stderr, "%s ", threadname);
}
if (file)
{
fprintf(stderr, "%s line %d: ", file, line);
@ -164,6 +178,10 @@ print(const char* fmt, ...)
StreamPrintTimestampFunction(timestamp, sizeof(timestamp));
fprintf(fp, "%s ", timestamp);
}
if (StreamGetThreadNameFunction)
{
fprintf(fp, "%s ", StreamGetThreadNameFunction());
}
fprintf(fp, "%s:%d: ", f, line);
vfprintf(fp, fmt, args);
fflush(fp);

View File

@ -35,6 +35,7 @@ extern int streamError;
extern int streamDebugColored;
extern int streamMsgTimeStamped;
extern void (*StreamPrintTimestampFunction)(char* buffer, size_t size);
extern const char* (*StreamGetThreadNameFunction)(void);
void StreamError(int line, const char* file, const char* fmt, ...)
__attribute__((__format__(__printf__,3,4)));