Skip to content

Commit 9222ca2

Browse files
authored
Merge 0a20b7c into 007279c
2 parents 007279c + 0a20b7c commit 9222ca2

File tree

1 file changed

+46
-38
lines changed

1 file changed

+46
-38
lines changed

modules/libcom/src/error/errlog.c

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,15 @@ typedef struct listenerNode{
7070

7171
typedef struct {
7272
char *base;
73-
size_t pos;
73+
int pos;
74+
int nchar;
7475
} buffer_t;
7576

7677
static struct {
7778
/* const after errlogInit() */
78-
size_t maxMsgSize;
79+
int maxMsgSize;
7980
/* alloc size of both buffer_t::base */
80-
size_t bufSize;
81+
int bufSize;
8182
int errlogInitFailed;
8283

8384
epicsMutexId listenerLock;
@@ -98,8 +99,8 @@ static struct {
9899

99100
/* A loop counter maintained by errlogThread. */
100101
epicsUInt32 flushSeq;
101-
size_t nFlushers;
102-
size_t nLost;
102+
unsigned long nFlushers;
103+
unsigned long nLost;
103104

104105
/* 'log' and 'print' combine to form a double buffer. */
105106
buffer_t *log;
@@ -125,9 +126,14 @@ char* msgbufAlloc(void)
125126

126127
errlogInit(0);
127128
epicsMutexMustLock(pvt.msgQueueLock); /* matched in msgbufCommit() */
128-
if(pvt.bufSize - pvt.log->pos >= 1+pvt.maxMsgSize) {
129+
if(pvt.bufSize - pvt.log->pos - pvt.log->nchar >= 1+pvt.maxMsgSize) {
129130
/* there is enough space for the worst cast */
130131
ret = pvt.log->base + pvt.log->pos;
132+
if (pvt.log->nchar) {
133+
/* append to last message */
134+
return ret + 1 + pvt.log->nchar;
135+
}
136+
/* new message */
131137
ret[0] = ERL_STATE_WRITE;
132138
ret++;
133139
}
@@ -140,39 +146,44 @@ char* msgbufAlloc(void)
140146
}
141147

142148
static
143-
size_t msgbufCommit(size_t nchar, int localEcho)
149+
int msgbufCommit(int nchar, int localEcho)
144150
{
145151
int isOkToBlock = epicsThreadIsOkToBlock();
146152
int wasEmpty = pvt.log->pos==0;
147153
int atExit = pvt.atExit;
148-
char *start = pvt.log->base + pvt.log->pos;
154+
char *start = pvt.log->base + pvt.log->pos + pvt.log->nchar;
149155

150156
/* nchar returned by snprintf() is >= maxMsgSize when truncated */
151157
if(nchar >= pvt.maxMsgSize) {
152158
const char *trunc = "<<TRUNCATED>>\n";
153-
nchar = pvt.maxMsgSize - 1u;
159+
nchar = pvt.maxMsgSize - 1;
154160

155-
strcpy(start + 1u + nchar - strlen(trunc), trunc);
156-
/* assert(strlen(start+1u)==nchar); */
161+
strcpy(start + 1 + nchar - strlen(trunc), trunc);
162+
/* assert(strlen(start+1)==nchar); */
157163
}
158164

159-
start[1u + nchar] = '\0';
165+
start[1 + nchar] = '\0';
160166

161167
if(localEcho && isOkToBlock && atExit) {
162168
/* errlogThread is not running, so we print directly
163169
* and then abandon the buffer.
164170
*/
165171
fprintf(pvt.console, "%s", start);
166172

173+
} else if (start[nchar] != '\n') {
174+
/* incomplete message, prepare to append */
175+
pvt.log->nchar += nchar;
176+
167177
} else {
168-
start[0u] = ERL_STATE_READY | (localEcho ? ERL_LOCALECHO : 0);
178+
pvt.log->base[pvt.log->pos] = ERL_STATE_READY | (localEcho ? ERL_LOCALECHO : 0);
169179

170-
pvt.log->pos += 1u + nchar + 1u;
180+
pvt.log->pos += 1 + pvt.log->nchar + nchar + 1;
181+
pvt.log->nchar = 0;
171182
}
172183

173184
epicsMutexUnlock(pvt.msgQueueLock); /* matched in msgbufAlloc() */
174185

175-
if(wasEmpty && !atExit)
186+
if(wasEmpty && !atExit && !pvt.log->nchar)
176187
epicsEventMustTrigger(pvt.waitForWork);
177188

178189
if(localEcho && isOkToBlock && !atExit)
@@ -185,7 +196,7 @@ static
185196
void errlogSequence(void)
186197
{
187198
int wakeNext = 0;
188-
size_t seq;
199+
epicsUInt32 seq;
189200

190201
if (pvt.atExit)
191202
return;
@@ -203,7 +214,7 @@ void errlogSequence(void)
203214
}
204215

205216
pvt.nFlushers--;
206-
wakeNext = pvt.nFlushers!=0u;
217+
wakeNext = pvt.nFlushers!=0;
207218
epicsMutexUnlock(pvt.msgQueueLock);
208219

209220
if(wakeNext)
@@ -234,22 +245,19 @@ int isATTY(FILE* fp)
234245
static
235246
int isATTY(FILE* fp)
236247
{
248+
#ifdef ENABLE_VIRTUAL_TERMINAL_PROCESSING
237249
HANDLE hand = NULL;
238250
DWORD mode = 0;
239251
if(fp==stdout)
240252
hand = GetStdHandle(STD_OUTPUT_HANDLE);
241253
else if(fp==stderr)
242254
hand = GetStdHandle(STD_ERROR_HANDLE);
243-
#ifdef ENABLE_VIRTUAL_TERMINAL_PROCESSING
244255
if(hand && GetConsoleMode(hand, &mode)) {
245256
(void)SetConsoleMode(hand, mode|ENABLE_VIRTUAL_TERMINAL_PROCESSING);
246-
mode = 0u;
257+
mode = 0;
247258
if(GetConsoleMode(hand, &mode) && (mode&ENABLE_VIRTUAL_TERMINAL_PROCESSING))
248259
return 1;
249260
}
250-
#else
251-
(void)hand;
252-
(void)mode;
253261
#endif
254262
return 0;
255263
}
@@ -263,7 +271,7 @@ void errlogStripANSI(char *msg);
263271

264272
void errlogStripANSI(char *msg)
265273
{
266-
size_t pos = 0, shift = 0;
274+
int pos = 0, shift = 0;
267275

268276
while(1) {
269277
char c = msg[pos];
@@ -517,8 +525,8 @@ static void errlogExitHandler(void *raw)
517525
}
518526

519527
struct initArgs {
520-
size_t bufsize;
521-
size_t maxMsgSize;
528+
int bufsize;
529+
int maxMsgSize;
522530
};
523531

524532
static void errlogInitPvt(void *arg)
@@ -616,7 +624,7 @@ static void errlogThread(void)
616624
while (!pvt.atExit) {
617625
pvt.flushSeq++;
618626

619-
if(pvt.log->pos==0u) {
627+
if(pvt.log->pos==0) {
620628
int wakeFlusher = pvt.nFlushers!=0;
621629
epicsMutexUnlock(pvt.msgQueueLock);
622630
if(wakeFlusher)
@@ -626,10 +634,10 @@ static void errlogThread(void)
626634

627635
} else {
628636
/* snapshot and swap buffers for use while unlocked */
629-
size_t nLost = pvt.nLost;
637+
unsigned long nLost = pvt.nLost;
630638
FILE *console = pvt.toConsole ? pvt.console : NULL;
631639
int ttyConsole = pvt.ttyConsole;
632-
size_t pos = 0u;
640+
int pos = 0;
633641
buffer_t *print;
634642

635643
{
@@ -638,49 +646,49 @@ static void errlogThread(void)
638646
pvt.print = print = temp;
639647
}
640648

641-
pvt.nLost = 0u;
649+
pvt.nLost = 0;
642650
epicsMutexUnlock(pvt.msgQueueLock);
643651

644652
while(pos < print->pos) {
645653
listenerNode *plistenerNode;
646654
char* base = print->base + pos;
647-
size_t mlen = epicsStrnLen(base+1u, pvt.bufSize - pos);
655+
int mlen = (int)epicsStrnLen(base+1, pvt.bufSize - pos);
648656
int stripped = 0;
649657

650658
if((base[0]&ERL_STATE_MASK) != ERL_STATE_READY || mlen>=pvt.bufSize - pos) {
651-
fprintf(stderr, "Logic Error: errlog buffer corruption. %02x, %zu\n",
659+
fprintf(stderr, "Logic Error: errlog buffer corruption. %02x, %u\n",
652660
(unsigned)base[0], mlen);
653661
/* try to reset and recover */
654662
break;
655663
}
656664

657665
if(base[0]&ERL_LOCALECHO && console) {
658666
if(!ttyConsole) {
659-
errlogStripANSI(base+1u);
667+
errlogStripANSI(base+1);
660668
stripped = 1;
661669
}
662-
fprintf(console, "%s", base+1u);
670+
fprintf(console, "%s", base+1);
663671
}
664672

665673
if(!stripped)
666-
errlogStripANSI(base+1u);
674+
errlogStripANSI(base+1);
667675

668676
epicsMutexMustLock(pvt.listenerLock);
669677
plistenerNode = (listenerNode *)ellFirst(&pvt.listenerList);
670678
while (plistenerNode) {
671-
(*plistenerNode->listener)(plistenerNode->pPrivate, base+1u);
679+
(*plistenerNode->listener)(plistenerNode->pPrivate, base+1);
672680
plistenerNode = (listenerNode *)ellNext(&plistenerNode->node);
673681
}
674682
epicsMutexUnlock(pvt.listenerLock);
675683

676-
pos += 1u + mlen+1u;
684+
pos += 1 + mlen + 1;
677685
}
678686

679687
memset(print->base, 0, pvt.bufSize);
680-
print->pos = 0u;
688+
print->pos = 0;
681689

682690
if(nLost && console)
683-
fprintf(console, "errlog: lost %zu messages\n", nLost);
691+
fprintf(console, "errlog: lost %lu messages\n", nLost);
684692

685693
if(console)
686694
fflush(console);

0 commit comments

Comments
 (0)