Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added logger #12

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/CommandBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@
Robot* CommandBase::robot = nullptr;

CommandBase::CommandBase(const char* name) :
Command(name) {
Command(name), logger(name) {

}

void CommandBase::Initialize() {
std::cout << GetName() << " command initialized" << std::endl;
logger.State(GetName() + " command initialized");
}

void CommandBase::End() {
std::cout << GetName() << " command ended" << std::endl;
logger.State(GetName() + " command ended");
}

void CommandBase::Interrupted() {
std::cout << GetName() << " command interrupted" << std::endl;
logger.State(GetName() + " command interrupted");
}

2 changes: 2 additions & 0 deletions src/CommandBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class CommandBase: public Command {
virtual bool IsFinished() override = 0;
virtual void End() override;
virtual void Interrupted() override;
private:
Logger logger;
};

#endif /* SRC_COMMANDBASE_H_ */
33 changes: 33 additions & 0 deletions src/Logger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <Logger.h>

#include "Timer.h"
#include <iostream>
#include <iomanip>

Logger::Logger(const std::string& loggerSource) {
this->source = loggerSource;
}

void Logger::Info(const std::string& message) {
std::cout << std::fixed << std::setprecision(3) << std::setw(8)
<< Timer::GetFPGATimestamp() << " [INFO] " << source << " - "
<< message << std::endl;
}

void Logger::Warn(const std::string& message) {
std::cout << std::fixed << std::setprecision(3) << std::setw(8)
<< Timer::GetFPGATimestamp() << " [WARN] " << source << " - "
<< message << std::endl;
}

void Logger::Error(const std::string& message) {
std::cout << std::fixed << std::setprecision(3) << std::setw(8)
<< Timer::GetFPGATimestamp() << " [ERROR] " << source << " - "
<< message << std::endl;
}

void Logger::State(const std::string& message) {
std::cout << std::fixed << std::setprecision(3) << std::setw(8)
<< Timer::GetFPGATimestamp() << " [STATE] " << source << " - "
<< message << std::endl;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please refactor this into a separate function

}
17 changes: 17 additions & 0 deletions src/Logger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef SRC_LOGGER_H_
#define SRC_LOGGER_H_

#include <string>

class Logger {
public:
Logger(const std::string& loggerSource);
void Info(const std::string& message);
void Warn(const std::string& message);
void Error(const std::string& message);
void State(const std::string& message);
private:
std::string source;
};

#endif /* SRC_LOGGER_H_ */
13 changes: 7 additions & 6 deletions src/Robot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
#include "Commands/DriveLowGear.h"
#include "Commands/DriveHighGear.h"

Robot::Robot() {
Robot::Robot() :
logger("Robot") {
// define robot pointer
CommandBase::robot = this;
}

// robot init implementation
void Robot::RobotInit() {
std::cout << "Entered RobotInit()" << std::endl;
logger.Info("Initializing robot");

scheduler = Scheduler::GetInstance();

Expand All @@ -37,23 +38,23 @@ void Robot::RobotInit() {

// disabled init implementation
void Robot::DisabledInit() {
std::cout << "Entered DisabledInit()" << std::endl;
logger.State("Entering disabled mode");
}

// autonomous init implementation
void Robot::AutonomousInit() {
std::cout << "Entered AutonomousInit()" << std::endl;
logger.State("Entering autonomous mode");
}

// teleop init implementation
void Robot::TeleopInit() {
std::cout << "Entered TeleopInit()" << std::endl;
logger.State("Entering teleop mode");
driveCommand->Start();
}

// test init implementation
void Robot::TestInit() {
std::cout << "Entered TestInit()" << std::endl;
logger.State("Entering test mode");
}

// disabled periodic implementation
Expand Down
2 changes: 2 additions & 0 deletions src/Robot.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define SRC_ROBOT_H_

#include <IterativeRobot.h>
#include "Logger.h"

class Drive;
class OI;
Expand Down Expand Up @@ -29,6 +30,7 @@ class Robot: public IterativeRobot {

private:
Scheduler* scheduler;
Logger logger;
};

#endif /* SRC_ROBOT_H_ */
5 changes: 5 additions & 0 deletions src/SubsystemBase.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <SubsystemBase.h>

SubsystemBase::SubsystemBase(const char* name) :
Subsystem(name), logger(name) {
}
14 changes: 14 additions & 0 deletions src/SubsystemBase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef SRC_SUBSYSTEMBASE_H_
#define SRC_SUBSYSTEMBASE_H_

#include <Commands/Subsystem.h>
#include "Logger.h"

class SubsystemBase: public Subsystem {
public:
SubsystemBase(const char* name);
private:
Logger logger;
};

#endif /* SRC_SUBSYSTEMBASE_H_ */
4 changes: 2 additions & 2 deletions src/Subsystems/Drive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <DoubleSolenoid.h>

Drive::Drive() :
Subsystem("Drive") {
SubsystemBase("Drive") {
left1 = new Victor(0);
left2 = new Victor(1);
left3 = new Victor(2);
Expand Down Expand Up @@ -39,6 +39,6 @@ void Drive::SetHighGear() {
gearSolenoid->Set(DoubleSolenoid::kForward);

}
void Drive::SetLowGear(){
void Drive::SetLowGear() {
gearSolenoid->Set(DoubleSolenoid::kReverse);
}
4 changes: 2 additions & 2 deletions src/Subsystems/Drive.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#ifndef SRC_SUBSYSTEMS_DRIVE_H_
#define SRC_SUBSYSTEMS_DRIVE_H_

#include <Commands/Subsystem.h>
#include "SubsystemBase.h"

class Victor;
class DoubleSolenoid;

class Drive: public Subsystem {
class Drive: public SubsystemBase {
public:
Drive();
~Drive();
Expand Down
2 changes: 1 addition & 1 deletion src/Subsystems/OI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <Buttons/JoystickButton.h>
#include "OI.h"
OI::OI() :
Subsystem("OI") {
SubsystemBase("OI") {
driverJoystick = new Joystick(0);

}
Expand Down
7 changes: 5 additions & 2 deletions src/Subsystems/OI.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#ifndef SRC_SUBSYSTEMS_OI_H_
#define SRC_SUBSYSTEMS_OI_H_

#include "SubsystemBase.h"

class Joystick;
class JoystickButton;
#include <Commands/Subsystem.h>
class OI: public Subsystem {

class OI: public SubsystemBase {
public:
OI();
~OI();
Expand Down