* @brief Handle stack trace messages. * * @ingroup lib_internal * * Maintains an array of debug messages to build a stack trace of up to 20 * lines. * * @sa rigerror() */
| 424 | * @sa rigerror() |
| 425 | */ |
| 426 | void add2debugmsgsave(const char *s) |
| 427 | { |
| 428 | const char *p; |
| 429 | char stmp[DEBUGMSGSAVE_SIZE]; |
| 430 | int i, nlines; |
| 431 | int maxmsg = DEBUGMSGSAVE_SIZE / 2; |
| 432 | MUTEX_LOCK(mutex_debugmsgsave); |
| 433 | memset(stmp, 0, sizeof(stmp)); |
| 434 | |
| 435 | // we'll keep 20 lines including this one |
| 436 | // so count the lines |
| 437 | for (i = 0, nlines = 0; debugmsgsave[i] != 0; ++i) |
| 438 | { |
| 439 | if (debugmsgsave[i] == '\n') { ++nlines; } |
| 440 | } |
| 441 | |
| 442 | // strip the last 19 lines |
| 443 | p = debugmsgsave; |
| 444 | |
| 445 | while ((nlines > 19 || strlen(debugmsgsave) > maxmsg) && p != NULL) |
| 446 | { |
| 447 | p = strchr(debugmsgsave, '\n'); |
| 448 | |
| 449 | if (p && strlen(p + 1) > 0) |
| 450 | { |
| 451 | strcpy(stmp, p + 1); |
| 452 | strcpy(debugmsgsave, stmp); |
| 453 | } |
| 454 | else |
| 455 | { |
| 456 | debugmsgsave[0] = '\0'; |
| 457 | } |
| 458 | |
| 459 | --nlines; |
| 460 | |
| 461 | if (nlines == 0 && strlen(debugmsgsave) > maxmsg) { strcpy(debugmsgsave, "!!!!debugmsgsave too long\n"); } |
| 462 | } |
| 463 | |
| 464 | if (strlen(stmp) + strlen(s) + 1 < DEBUGMSGSAVE_SIZE) |
| 465 | { |
| 466 | strcat(debugmsgsave, s); |
| 467 | } |
| 468 | else |
| 469 | { |
| 470 | rig_debug(RIG_DEBUG_BUG, |
| 471 | "%s: debugmsgsave overflow!! len of debugmsgsave=%d, len of add=%d\n", __func__, |
| 472 | (int)strlen(debugmsgsave), (int)strlen(s)); |
| 473 | } |
| 474 | |
| 475 | MUTEX_UNLOCK(mutex_debugmsgsave); |
| 476 | } |
| 477 | |
| 478 | |
| 479 | /** |