Commits

mackx committed 1aeb391

Adding battery logging functionality

  • Participants
  • Parent commits 12aa9ab

Comments (0)

Files changed (5)

File sysd/include/logger.h

+#ifndef __LOGGER_H__
+#define __LOGGER_H__
+
+
+/* FUNCTION: loggerCreate
+ *
+ * Create a new logger, reserves memory to store the logs into
+ * memory. When the buffer is full or when a flush is called the
+ * data is written to disk.
+ */
+void loggerCreate();
+
+
+/* FUNCTION: loggerFlush
+ *
+ * Store the current content of the internal buffers to disk.
+ */
+void loggerFlush();
+
+/* FUNCTION: loggerLogString
+ *
+ * Log the message in the internal buffer with a timestamp!
+ */
+void loggerLogString(char *str);
+
+/* FUNCTION: loggerLogIntInt
+ *
+ * Log the values for state and level in the internal buffer
+ */
+void loggerLogIntInt(int state, int level);
+
+
+#endif // __LOGGER_H__

File sysd/src/Makefile.am

         system.c                            \
         tasks.c                             \
         wacom.c                             \
-        xwindow.c
+        xwindow.c                           \
+        logger.c
 
 blank_SOURCES = blank.c
 blank_CFLAGS = -Wall -Werror -I$(top_srcdir)/include

File sysd/src/logger.c

+/*
+ * File Name: logger.c
+ */
+
+/*
+ * logger.c is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * logger.c is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/**
+ * Copyright (C) 2011 Marcel Hendrickx
+ * All rights reserved.
+ */
+
+// TODO: Create a mechanism that also flushes the data when a certain amount
+//       has passed since the last flush. (can timestamp be used?).
+// TODO: Also log sys_restart()? It is not used much, so makes not much sense?
+// TODO: Currently data is lost during a reboot, can that be detected?
+//       Maybe ipc call can be added?
+//----------------------------------------------------------------------------
+// Include Files
+//----------------------------------------------------------------------------
+
+#include <stdio.h>
+#include <string.h>
+#include <time.h>
+
+//----------------------------------------------------------------------------
+// Type Declarations
+//----------------------------------------------------------------------------
+
+typedef unsigned long long u_int64_t;
+
+//----------------------------------------------------------------------------
+// Global Constants
+//----------------------------------------------------------------------------
+
+#define LOGGER_BUFFER_SIZE 4096
+
+//----------------------------------------------------------------------------
+// Static Variables
+//----------------------------------------------------------------------------
+
+// the internal buffer
+static char loggerBuffer[LOGGER_BUFFER_SIZE];
+
+// current write pointer into internal buffer
+static int iWrite = 0;
+
+//============================================================================
+// Local Function Definitions
+//============================================================================
+
+//============================================================================
+// Functions Implementation
+//============================================================================
+
+/* FUNCTION: loggerCreate
+ *
+ * Create a new logger, reserves memory to store the logs into
+ * memory. When the buffer is full or when a flush is called the
+ * data is written to disk.
+ */
+void loggerCreate()
+{
+    // reset write pointer
+    iWrite = 0;
+    loggerBuffer[iWrite] = '\0'; // clear
+}
+
+/* FUNCTION: loggerFlush
+ *
+ * Store the current content of the internal buffers to disk.
+ */
+void loggerFlush()
+{
+    FILE *fplog;
+    
+    // This function should only be called when an SD card is available
+    // since I do not want to log in the internal flash
+    
+    // Check if file exists. Only log if file exists!!!
+    fplog = fopen("/media/mmcblk0p1/log.txt", "r");
+    if (fplog)
+    {
+        fclose(fplog); // close file to reopen in append mode
+        fplog = fopen("/media/mmcblk0p1/log.txt", "a");
+        if (fplog)
+        {
+            //printf("LOGGER: Writing %d butes\n", strlen(loggerBuffer));
+            // TODO: make sure that file does not get too big?
+            fwrite(loggerBuffer, 1, strlen(loggerBuffer), fplog);
+            fclose(fplog);
+
+            // reset write pointer
+            iWrite = 0;
+            loggerBuffer[iWrite] = '\0'; // clear
+        }
+        // else could not write, but can not help, at least keep data for next try
+        // TODO: check buffer fill, if too full, remove some data to keep the latest
+        //       data incase writing succeeds next time
+    }
+    // TODO: check buffer fill, if too full, remove some data to keep the latest
+    //       data incase logging is enabled next time
+}
+
+/* FUNCTION: loggerLogString
+ *
+ * Log the message in the internal buffer with a timestamp!
+ */
+void loggerLogString(char *str)
+{
+    // determine if message fits
+    if ((iWrite + strlen(str) + 15) > LOGGER_BUFFER_SIZE)
+    {
+        // flush buffer before logging
+        loggerFlush();
+    }
+    
+    // Check again, incase flush failed
+    if ((iWrite + strlen(str) + 15) < LOGGER_BUFFER_SIZE)
+    {
+        //struct tm *local;
+        time_t now;
+
+        now = time(NULL);
+        //local = localtime(&now);
+        int size = sprintf(&loggerBuffer[iWrite], "%ld:%s;\n", now, str);
+        //int size = sprintf(&loggerBuffer[iWrite], "%2.2d:%2.2d:%2.2d:%s;\n", local->tm_hour, local->tm_min, local->tm_sec, str);
+        //printf("LOGGER:%s\n", &loggerBuffer[iWrite]);
+        iWrite += size;
+        loggerBuffer[iWrite]='\0';
+    }
+}
+
+/* FUNCTION: loggerLogIntInt
+ *
+ * Log the values for state and level in the internal buffer
+ */
+void loggerLogIntInt(int state, int level)
+{
+    // determine if message fits
+    if ((iWrite + 20 + 15) > LOGGER_BUFFER_SIZE)
+    {
+        // flush buffer before logging
+        loggerFlush();
+    }
+    
+    // Check again, incase flush failed
+    if ((iWrite + 20 + 15) < LOGGER_BUFFER_SIZE)
+    {
+        //struct tm *local;
+        time_t now;
+
+        now = time(NULL);
+        //local = localtime(&now);
+        int size = sprintf(&loggerBuffer[iWrite], "%ld:B%d,%d;\n", now, state, level);
+        //printf("LOGGER:%s\n", &loggerBuffer[iWrite]);
+        iWrite += size;
+        loggerBuffer[iWrite]='\0';
+    }
+}
+
+

File sysd/src/main.c

 #include "tasks.h"
 #include "xwindow.h"
 
+#include "logger.h"
 
 //----------------------------------------------------------------------------
 // Type Declarations
 {
     struct sigaction on_term;
     
+    loggerLogString("PB");
     // parse command-line arguments
     parse_arguments(argc, argv);
 

File sysd/src/system.c

 #include "xwindow.h"
 #include "tasks.h"
 
+#include "logger.h"
 
 //----------------------------------------------------------------------------
 // Type Declarations
 
 gboolean sys_set_sensor_lock_config(gboolean lock_left, gboolean lock_right, gboolean lock_middle)
 {
-    WARNPRINTF("entry");
+    LOGPRINTF("entry");
     
     // NOTE: the micro has a sensor_enable setting so we invert the 
     //       lock to enable logic here
 
 gboolean sys_set_sensor_startup_config(gboolean enable_after_restart, gboolean state_before_shutdown)
 {
-    WARNPRINTF("entry: %d %d", (int)enable_after_restart, (int)state_before_shutdown);
+    LOGPRINTF("entry: %d %d", (int)enable_after_restart, (int)state_before_shutdown);
     
     if (enable_after_restart && state_before_shutdown)
     {
 
 gboolean sys_set_sensor_lock(const char *lock_mode)
 {
-    WARNPRINTF("entry: %s\n", lock_mode);
+    LOGPRINTF("entry: %s\n", lock_mode);
     
     g_return_val_if_fail(lock_mode!=NULL, FALSE);
     
   
     if (is_changed)
     {
-        WARNPRINTF("status:%d", g_sensor_locked);
+        LOGPRINTF("status:%d", g_sensor_locked);
         if (g_sensor_locked)
         {
             // inform ionkbd
         return;
     }
     
+    loggerLogIntInt(charge_state, g_battery_level);
     if (charge_state == STATE_CHARGE_CHARGING)
     {
         // Battery Charging
 
     // synchronize with Adobe Digital Editions
     sys_spawn_sync(ADOBE_SYNC);
+    
+    loggerLogString("CU");
+    loggerLogIntInt(charge_state, g_battery_level);
+    loggerFlush();
 
     // unmount volumes
     hal_unmount_all_volumes(callback_function, user_data);
     
     if (device_state != STATE_DEVICE_STOPPING)
     {
+        loggerLogString("PF");
+        
         device_state = STATE_DEVICE_STOPPING;
         busy_add_foreground(0, BUSY_DIALOG_NONE, NULL);
         if (card_state == STATE_CARD_MOUNTED)
         write_ionkbd("fastcharge", "1");
     }
     
+    loggerLogString("PS");
+    
     sys_spawn_sync("standby.sh");
    
     //
     //
     
     LOGPRINTF("Back from standby");
+    loggerLogString("PNS");
 
     gint reason = get_wakeup_reason();
     if ( reason == WAKEUP_BATTERY )
             
         if (min_before_standby > 0)
         {
+            loggerLogString("PI");
             // call idle script and wait for it to return
             gchar *command = g_strdup_printf("idle.sh %d", min_before_standby);
             sys_spawn_sync(command);
     }
     else
     {
+        loggerLogString("PI");
         // call idle script and wait for it to return
         LOGPRINTF("Enter idle without standby");
         sys_spawn_sync("idle.sh");
     //
     
     LOGPRINTF("Back from idle");
+    loggerLogString("PNI");
 
     gint reason = get_wakeup_reason();
     if ( (reason == WAKEUP_TIMER) &&