//****************************************************************************** /// \brief Get messages /// \param mxt Maxtouch Device /// \param count Number of messages available /// \param init_timestamp Read newest dmesg line and initialise timestamp /// \return #mxt_rc int dmesg_get_msgs(struct mxt_device *mxt, int *count, bool init_timestamp) { char msg[BUFFERSIZE]; int ep, sp; int ret = MXT_SUCCESS; unsigned long sec, msec, lastsec = 0, lastmsec = 0; // Read entire kernel log buffer ep = klogctl(SYSLOG_ACTION_READ_ALL, mxt->sysfs.debug_msg_buf, mxt->sysfs.debug_msg_buf_size); // Return if no bytes read if (ep < 0) { mxt_warn(mxt->ctx, "klogctl error %d (%s)", errno, strerror(errno)); ret = mxt_errno_to_rc(errno); } else { // null terminate mxt->sysfs.debug_msg_buf[ep] = 0; sp = ep; if (!init_timestamp) dmesg_list_empty(mxt); // Search for next new line character while (true) { sp--; while (sp >= 0 && *(mxt->sysfs.debug_msg_buf + sp) != '\n') sp--; if (sp <= 0) break; // Try to parse dmesg line if (sscanf(mxt->sysfs.debug_msg_buf+sp+1, "< %*c>[ %lu.%06lu] %255[^\n]", &sec, &msec, msg) == 3) { if (init_timestamp) { mxt->sysfs.timestamp = sec; mxt->sysfs.mtimestamp = msec; mxt_verb(mxt->ctx, "%s - init [%5lu.%06lu]", __func__, sec, msec); break; } // Store time of last message in buffer if (lastsec == 0) { lastsec = sec; lastmsec = msec; } // Only 500 at a time, otherwise we overrun JNI reference limit. // Timestamp must be greater than previous messages, slightly // complicated by seconds and microseconds if ((mxt->sysfs.dmesg_count > MAX_DMESG_COUNT) || (sec == mxt->sysfs.timestamp && msec <= mxt->sysfs.mtimestamp) || (sec < mxt->sysfs.timestamp)) { mxt->sysfs.timestamp = lastsec; mxt->sysfs.mtimestamp = lastmsec; break; } char* msgptr; msg[sizeof(msg) - 1] = '\0'; msgptr = strstr(msg, "MXT MSG"); if (msgptr) dmesg_list_add(mxt, sec, msec, msgptr); } } if (!init_timestamp) { *count = mxt->sysfs.dmesg_count; mxt->sysfs.dmesg_ptr = mxt->sysfs.dmesg_head; } } return ret; }
void dmesg_dump() { char buffer[KLOG_BUF_LEN + 1]; char line[BUFFERSIZE*2]; char *lineptr; int n, op, sp, ep; dmesg_item cur_dmesg; op = KLOG_READ_ALL; op = klogctl(op, buffer, KLOG_BUF_LEN); if (op < 0) return; dmesg_list_empty(); pre_dmesg_time = cur_dmesg_time; buffer[op] = 0; sp = ep = 0; while(((int)(lineptr = strstr(buffer+sp, "\n" )) != 0)) { ep = lineptr - buffer - sp; if(ep > BUFFERSIZE) ep = BUFFERSIZE; strncpy(line, buffer+sp, ep); line[ep+1] = '\0'; sp = sp + ep +1; cur_dmesg.sec = 0; n = 0; if(line[3] == '[') n = sscanf(line, "<%c>[%lu.%*06lu] %[^\n]", &cur_dmesg.level, &cur_dmesg.sec, cur_dmesg.msg); else n = sscanf(line, "<%c>%[^\n]", &cur_dmesg.level, cur_dmesg.msg); cur_dmesg.msg[strlen(cur_dmesg.msg)-1] = '\0'; if(n > 0) { int filter_item = 1; if(filter_dmesg == 1) { if(filter_dmesg_level != '8') if(cur_dmesg.level != filter_dmesg_level) filter_item = 0; if(strlen(filter_dmesg_string) != 0) if(strstr(cur_dmesg.msg, filter_dmesg_string) == 0) filter_item = 0; } if(filter_item == 1) { cur_dmesg_time = cur_dmesg.sec; dmesg_list_add(&cur_dmesg); } } if(sp >= op) break; } }