Commit 8fc2a42a authored by 刘睿博's avatar 刘睿博 🎯

lab0 published

parent 81c4e3e5
build/
.cache/
\ No newline at end of file
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug",
"program": "${workspaceFolder}/build/src/lab0_debug",
"args": [
"-t",
],
"cwd": "${workspaceFolder}"
}
]
}
\ No newline at end of file
cmake_minimum_required(VERSION 3.5.0)
project(stl_debug VERSION 0.1.0 LANGUAGES C CXX)
set(CMAKE_CXX_STANDARD 17)
set(default_build_type "Debug")
INCLUDE_DIRECTORIES(
include
)
add_subdirectory(src)
- Lab0 已经发布 `git checkout lab0`
\ No newline at end of file
#pragma once
#include <string>
class Human
{
private:
/* data */
std::string name_;
int age_;
public:
explicit Human(int age, const std::string name = "") : name_(name), age_(age) {};
virtual ~Human();
virtual std::string print() const;
};
#pragma once
#include "Human.hpp"
#include <string>
class Student : public Human
{
private:
/* data */
std::string school_;
public:
Student() = delete;
explicit Student(int age, const std::string name = "", const std::string school = "") : Human(age, name), school_(school) {};
virtual ~Student() override;
virtual std::string 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(
lab0_debug main.cpp Human.cpp Student.cpp logging.cpp
)
\ No newline at end of file
#include "Human.hpp"
#include <string>
#include <sstream>
Human::~Human() {
printf("Human destructor called\n");
}
std::string Human::print() const {
std::string res;
std::stringstream ss;
ss << "My name is " << name_ << " and I am " << age_ << " years old\n";
ss >> res;
return res;
}
\ No newline at end of file
#include "Student.hpp"
#include "Human.hpp"
#include "cstring"
#include <sstream>
#include <string>
Student::~Student() {
printf("Student destructor called\n");
}
std::string Student::print() const {
std::string res;
std::stringstream ss;
ss << Human::print();
ss << "I'm from " << school_ << "\n";
ss >> res;
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 <iostream>
#include <string>
#include <stdio.h>
#include "Human.hpp"
#include "Student.hpp"
#include "logging.hpp"
#include <vector>
class check{
public:
explicit check(Student* student){
printf("Student object created\n");
}
};
int main(int argc, char** argv){
if(argc == 1 || std::string(argv[1]) != "-t"){
std::cerr << "error: invalid argument\n";
exit(-1);
}
printf("Hello, from stl_debug!\n");
std::vector<Human*> vec;
Human human(25, "John Doe");
Student student(20, "Jane Doe", "MIT");
vec.push_back(&human);
vec.push_back(&student);
for (const auto& h : vec) {
std::cout << h->print();
}
// auto student1 = new Student(); //TODO: Error 1 : why?
auto student1 = static_cast<Student*>(vec.back());
// check check1 = check(student); //TODO: Error 2 : why?
check check1 = check(student1);
// LOG(DEBUG) << student1->print();
// LOG(WARNING) << human.print();
// std::list<int> list;
// list.push_back(1);
// for(auto i: list){
// list.remove(i);
// }
return 0;
}
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