-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
171 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
28 changes: 0 additions & 28 deletions
28
n4-flight-software/system-logger/logger-arduino/logger-arduino.ino
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.