Skip to content

Commit

Permalink
Improve error transparency
Browse files Browse the repository at this point in the history
  • Loading branch information
SuppenGeist committed May 22, 2016
1 parent c3cb1be commit f864204
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 14 deletions.
20 changes: 17 additions & 3 deletions src/ak_logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,35 @@ AKLogger::~AKLogger() {
}

void AKLogger::addOutputStream(OutputStream* ostream){
if(!isActive)
return;

if(ostream == nullptr)
return;

if(ostream->open()) {
Serial.println("opened");
int err = ostream->open();
if(err == AKSTREAM_SUCC) {
Serial.println("Successully opened '"+ostream->getName()+"'");
ostreams.push_back(ostream);
} else {
Serial.println("Failed to open '"+ostream->getName()+"': "+String(err));
}
}

void AKLogger::addInputStream(InputStream* istream) {
if(!isActive)
return;

if(istream == nullptr)
return;

if(istream->open())
int err = istream->open();
if(err == AKSTREAM_SUCC) {
Serial.println("Successully opened '"+istream->getName()+"'");
istreams.push_back(istream);
} else {
Serial.println("Failed to open '"+istream->getName()+"': "+String(err));
}
}

void AKLogger::logLineFormat() {
Expand Down
6 changes: 6 additions & 0 deletions src/akstream.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
#pragma once

#include <Arduino.h>

#define AKSTREAM_SUCC 0

class AKStream {
public:
virtual ~AKStream() {

}

virtual String getName()=0;

virtual int open()=0;
virtual int close()=0;
};
8 changes: 6 additions & 2 deletions src/console_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ ConsoleStream::~ConsoleStream() {

int ConsoleStream::open() {
//Expects the Serial output already opened with Serial.begin
return CONSOLE_SUCC;
return AKSTREAM_SUCC;
}

int ConsoleStream::close() {
//Serial.end should not be called since Serial might
//still be used somewhere else.
//We didnt open it so we dont close it.
return CONSOLE_SUCC;
return AKSTREAM_SUCC;
}

void ConsoleStream::writeLine(String line) {
Expand All @@ -29,3 +29,7 @@ void ConsoleStream::writeLine(String line) {
void ConsoleStream::flush() {
Serial.flush();
}

String ConsoleStream::getName() {
return "ConsoleStream";
}
4 changes: 2 additions & 2 deletions src/console_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

#include "output_stream.h"

#define CONSOLE_SUCC 0

class ConsoleStream: public OutputStream {
public:
ConsoleStream();
Expand All @@ -16,4 +14,6 @@ class ConsoleStream: public OutputStream {

void writeLine(String line);
void flush();

String getName();
};
8 changes: 6 additions & 2 deletions src/sdcard_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ int SDCardStream::open() {
}

isOpened = true;
return SD_CARD_SUCC;
return AKSTREAM_SUCC;
}

int SDCardStream::close() {
return SD_CARD_SUCC;
return AKSTREAM_SUCC;
}

void SDCardStream::writeLine(String line) {
Expand All @@ -40,3 +40,7 @@ void SDCardStream::writeLine(String line) {
void SDCardStream::flush() {
//Nothing to do
}

String SDCardStream::getName() {
return "SDCardStream";
}
3 changes: 2 additions & 1 deletion src/sdcard_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#define SD_CARD_PIN 14
#define ERR_INIT_SD_CARD 1
#define SD_CARD_SUCC 0

class SDCardStream: public OutputStream {
public:
Expand All @@ -19,6 +18,8 @@ class SDCardStream: public OutputStream {
void writeLine(String line);
void flush();

String getName();

private:
String filename;
bool isOpened;
Expand Down
8 changes: 6 additions & 2 deletions src/sensirion_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ SensirionStream::~SensirionStream() {
int SensirionStream::open() {
sensor.reset();

return SENSIRION_SUCC;
return AKSTREAM_SUCC;
}

int SensirionStream::close() {
sensor.reset();

return SENSIRION_SUCC;
return AKSTREAM_SUCC;
}

String SensirionStream::getLineFormat() {
Expand All @@ -43,3 +43,7 @@ String SensirionStream::readNextLine() {

return result;
}

String SensirionStream::getName() {
return "SensirionStream";
}
4 changes: 2 additions & 2 deletions src/sensirion_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#define SENSIRION_DATA_PIN 22
#define SENSIRION_CLOCK_PIN 23

#define SENSIRION_SUCC 0

#include <Sensirion.h>

#include "input_stream.h"
Expand All @@ -22,6 +20,8 @@ class SensirionStream: public InputStream {
String getLineFormat();
String readNextLine();

String getName();

private:
Sensirion sensor;

Expand Down

0 comments on commit f864204

Please sign in to comment.