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

View File

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

View File

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