Commit 093a32f0 authored by 刘睿博's avatar 刘睿博 🎯

Merge branch 'main' of github.com:USTC-Compiler-2024/stl_debug

parents 06cc9587 d9555656
......@@ -9,6 +9,6 @@ private:
public:
explicit Human(int age, const std::string name = "") : name_(name), age_(age) {};
virtual ~Human();
virtual void print() const;
virtual char* print() const;
};
......@@ -11,5 +11,5 @@ public:
Student() = delete;
explicit Student(int age, const std::string name = "", const std::string school = "") : Human(age, name), school_(school) {};
virtual ~Student() override;
virtual void print() const override;
virtual char* print() const override;
};
\ No newline at end of file
#ifndef LOGGING_HPP
#define LOGGING_HPP
#include <iostream>
#include <sstream>
#include <cstdlib>
enum LogLevel
{
DEBUG = 0,
INFO,
WARNING,
ERROR
};
struct LocationInfo
{
LocationInfo(std::string file, int line, const char *func) : file_(file), line_(line), func_(func) {}
~LocationInfo() = default;
std::string file_;
int line_;
const char *func_;
};
class LogStream;
class LogWriter;
class LogWriter
{
public:
LogWriter(LocationInfo location, LogLevel loglevel)
: location_(location), log_level_(loglevel)
{
char *logv = std::getenv("LOGV");
if (logv)
{
std::string string_logv = logv;
env_log_level = std::stoi(logv);
}
else
{
env_log_level = 4;
}
};
void operator<(const LogStream &stream);
private:
void output_log(const std::ostringstream &g);
LocationInfo location_;
LogLevel log_level_;
int env_log_level;
};
class LogStream
{
public:
template <typename T>
LogStream &operator<<(const T &val) noexcept
{
sstream_ << val;
return *this;
}
friend class LogWriter;
private:
std::stringstream sstream_{};
};
std::string level2string(LogLevel level);
std::string get_short_name(const char *file_path);
#define __FILESHORTNAME__ get_short_name(__FILE__)
#define LOG_IF(level) \
LogWriter(LocationInfo(__FILESHORTNAME__, __LINE__, __FUNCTION__), level) < LogStream()
#define LOG(level) LOG_##level
#define LOG_DEBUG LOG_IF(DEBUG)
#define LOG_INFO LOG_IF(INFO)
#define LOG_WARNING LOG_IF(WARNING)
#define LOG_ERROR LOG_IF(ERROR)
#endif
add_executable(
stl_test main.cpp Human.cpp Student.cpp
stl_test main.cpp Human.cpp Student.cpp logging.cpp
)
\ No newline at end of file
......@@ -4,6 +4,8 @@ Human::~Human() {
printf("Human destructor called\n");
}
void Human::print() const {
printf("My name is %s and I am %d years old\n", name_.c_str(), age_);
char* Human::print() const {
char* res = new char[100];
sprintf(res, "My name is %s and I am %d years old\n", name_.c_str(), age_);
return res;
}
\ No newline at end of file
#include "Student.hpp"
#include "cstring"
Student::~Student() {
printf("Student destructor called\n");
}
void Student::print() const {
Human::print();
printf("I'm from %s\n", school_.c_str());
char* Student::print() const {
char *res = Human::print();
sprintf(res + strlen(res), "I'm from %s\n", school_.c_str());
return res;
}
\ No newline at end of file
#include "logging.hpp"
void LogWriter::operator<(const LogStream &stream) {
std::ostringstream msg;
msg << stream.sstream_.rdbuf();
output_log(msg);
}
void LogWriter::output_log(const std::ostringstream &msg) {
if (log_level_ >= env_log_level)
std::cout << "[" << level2string(log_level_) << "] "
<< "(" << location_.file_
<< ":" << location_.line_
<< "L "<< location_.func_<<")"
<< msg.str() << std::endl;
}
std::string level2string(LogLevel level) {
switch (level)
{
case DEBUG:
return "DEBUG";
case INFO:
return "INFO";
case WARNING:
return "WARNING";
case ERROR:
return "ERROR";
default:
return "";
}
}
std::string get_short_name(const char * file_path) {
std::string short_file_path = file_path;
int index = short_file_path.find_last_of('/');
return short_file_path.substr(index+1);
}
#include <stdio.h>
#include "Human.hpp"
#include "Student.hpp"
#include "logging.hpp"
#include <vector>
class check{
......@@ -18,8 +19,11 @@ int main(int, char**){
vec.push_back(&human);
vec.push_back(&student);
for (const auto& h : vec) {
h->print();
std::cout << h->print();
}
auto student1 = static_cast<Student*>(vec.back());
// check check1 = check(student);
check check1 = check(student1);
LOG(DEBUG) << student1->print();
LOG(WARNING) << human.print();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment