Skip to content

Commit

Permalink
adds system logger to main cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
bytecod3 committed Aug 12, 2024
1 parent c6f70fe commit 1f7f996
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 146 deletions.
12 changes: 11 additions & 1 deletion mainpage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,17 @@
* @section step14 Integration and Testing Procedures
* @note In development

* @section step15 Contributors
* @section step15 Pre-flight software checks
* The following checks MUST be performed pre-flight to ensure the software operates as designed:
1. test mode pin toggle
2. telemetry protocol selection
3. clientID (rocket ID)
4. arming check
5. DEBUG_to_terminal check - defs.h
6. LOG_TO_MEMORY - defs.h


* @section step16 Contributors
* Our thanks go to the following current and previous avionics team members for their contribution:
1. Edwin Mwiti
2. Junn Hope
Expand Down
8 changes: 4 additions & 4 deletions n4-flight-software/include/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
#define XBEE 1 // set to 1 if using XBEE for telemetry transfer

#define GPS_BAUD_RATE 9600
#define XBEE_BAUD_RATE // TODO: set to XBEE baud rate
#define XBEE_BAUD_RATE 9600 // TODO: set to XBEE baud rate

/* debug parameters for use during testing - set to 0 for production */
#define DEBUG 1
#define LOG_TO_MEMORY 0 // set to 1 during live testing
#define DEBUGGING 1
#define LOG_TO_MEMORY 0 // set to 1 during live testing
#define DEBUG_TO_TERMINAL 1 // set to 0 for production

#if DEBUG
#if DEBUGGING
#define debug(x) Serial.print(x)
#define debugln(x) Serial.println(x)
#define debugf(x, y) Serial.printf(x, y)
Expand Down
2 changes: 1 addition & 1 deletion n4-flight-software/src/custom-time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ char tstamp[50]; // to hold a timestamp
int minute=0, sec=0, msec=0;

/*!****************************************************************************
* @brief convert time in millisecsonds to minutes, seconds and time that are human readable
* @brief convert time in milliseconds to minutes, seconds and time that are human readable
* @param msec time in milliseconds, got from millis() function
*******************************************************************************/
char* convertTimestamp(unsigned long msec) {
Expand Down
16 changes: 14 additions & 2 deletions n4-flight-software/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@
#include "data-types.h"
#include "custom-time.h"
#include "states.h"
#include "system_logger.h"
#include "system_log_levels.h"

/* function prototypes definition */
void drogueChuteDeploy();
void mainChuteDeploy();

/* state machine variables*/
uint8_t operation_mode = 0; /*!< Tells whether software is in safe or flight mode - FLIGHT_MODE=1, SAFE_MODE=0 */
uint8_t operation_mode = 0; /*!< Tells whether software is in safe or flight mode - FLIGHT_MODE=1, SAFE_MODE=0 */
uint8_t current_state = FLIGHT_STATE::PRE_FLIGHT_GROUND; /*!< The starting state - we start at PRE_FLIGHT_GROUND state */

/* create Wi-Fi Client */
Expand All @@ -46,6 +48,12 @@ PubSubClient mqtt_client(wifi_client);
/* GPS object */
TinyGPSPlus gps;

/* system logger */
SystemLogger system_logger;
const char* system_log_file = "/sys_log.log";
LOG_LEVEL level = INFO;
const char* rocket_ID = "rocket-1"; /*!< Unique ID of the rocket. Change to the needed rocket name before uploading */

//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////// FLIGHT COMPUTER TESTING SYSTEM /////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1203,7 +1211,7 @@ void mainChuteDeploy() {


/*!****************************************************************************
* @brief Setup - perfom initialization of all hardware subsystems, create queues, create queue handles
* @brief Setup - perform initialization of all hardware subsystems, create queues, create queue handles
* initialize system check table
*
*******************************************************************************/
Expand All @@ -1212,6 +1220,10 @@ void setup(){
Serial.begin(BAUDRATE);
delay(100);

/* initialize the system logger */
InitSPIFFS();
system_logger.logToFile(SPIFFS, 0, rocket_ID, level, system_log_file, "Game Time!");

debugln();
debugln(F("=============================================="));
debugln(F("========= INITIALIZING PERIPHERALS ==========="));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
*
*/

#ifndef LEVELS_H
#define LEVELS_H
#ifndef SYSTEM_LOG_LEVELS_H
#define SYSTEM_LOG_LEVELS_H

enum LOG_LEVEL {
typedef enum{
DEBUG = 0,
INFO,
WARNING,
CRITICAL,
ERROR
};
} LOG_LEVEL;

#endif
121 changes: 121 additions & 0 deletions n4-flight-software/src/system_logger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#include "system_logger.h"

void SystemLogger::logToFile (fs::FS &fs, uint8_t mode, const char* client, uint8_t log_level, const char* file, const char* msg) {
char log_buffer[100];
// get the timestamp
uint32_t timestamp = millis();

// construct the log message
// timestamp clientID log_level msg
sprintf(log_buffer,
"%d %s %s %s\n",
timestamp,
client,
this->getLogLevelString(log_level),
msg
);

// open the file in the specified mode
if(mode == 0) {
// clear the file contents
// this is used just before flight to make sure we do not have previous data
// on the log file
File f = fs.open(file, FILE_WRITE);
if(!f) {
Serial.println("Failed to open file ");
return;
}

// log to file
if(f.print(log_buffer)) {
Serial.println("- file written");
} else {
Serial.println("- write failed");
}

// close file
f.close();

} else if(mode == 1) {
File f = fs.open(file, FILE_APPEND);
if(!f) {
Serial.println("Failed to open file ");
return;
}

// log to file
if(f.print(log_buffer)) {
Serial.println("- file written");
} else {
Serial.println("- write failed");
}

// close file
f.close();

}
}

/**
* @brief read log file to console
*/
void SystemLogger::readLogFile(fs::FS &fs, const char* file) {
Serial.printf("Reading file: %s\r\n", file);
File f = fs.open(file);

if(!f || f.isDirectory()){
Serial.println("- failed to open file for reading");
return;
}

Serial.println("- read from file:");
while(f.available()){
Serial.write(f.read());
}
Serial.println();
f.close();
}

/**
*
* @brief convert the log level to string
*
*/
const char* SystemLogger::getLogLevelString(uint8_t log_level) {
static const char* debug = "DEBUG";
static const char* info = "INFO";
static const char* warning = "WARNING";
static const char* critical = "CRITICAL";
static const char* error = "ERROR";
static const char* unknown = "UNKNOWN";

switch (log_level) {
case 0:
return debug;
break;

case 1:
return info;
break;

case 2:
return warning;
break;

case 3:
return critical;
break;

case 4:
return error;
break;

default:
return unknown;
break;
}

}



16 changes: 16 additions & 0 deletions n4-flight-software/src/system_logger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef SYSTEMLOGGER_H
#define SYSTEMLOGGER_H

#include <Arduino.h>
#include <FS.h>
#include <SPIFFS.h>

class SystemLogger {
public:
const char* getLogLevelString(uint8_t log_level);
void logToConsole (const uint32_t timestamp, const char* client, uint8_t log_level, const char* msg);
void logToFile (fs::FS &fs, uint8_t mode, const char* client, uint8_t log_level, const char* file, const char* msg);
void readLogFile(fs::FS &fs, const char* file);
};

#endif
45 changes: 0 additions & 45 deletions n4-flight-software/system-logger/SystemLogger.cpp

This file was deleted.

13 changes: 0 additions & 13 deletions n4-flight-software/system-logger/SystemLogger.h

This file was deleted.

28 changes: 0 additions & 28 deletions n4-flight-software/system-logger/logger-arduino/logger-arduino.ino

This file was deleted.

48 changes: 0 additions & 48 deletions n4-flight-software/system-logger/logger-console.cpp

This file was deleted.

0 comments on commit 1f7f996

Please sign in to comment.