Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
2
2024ustc-jianmu-compiler
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
1
Issues
1
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Metrics
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
compiler_staff
2024ustc-jianmu-compiler
Commits
093a32f0
Commit
093a32f0
authored
Sep 01, 2024
by
刘睿博
🎯
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'main' of github.com:USTC-Compiler-2024/stl_debug
parents
06cc9587
d9555656
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
141 additions
and
9 deletions
+141
-9
include/Human.hpp
include/Human.hpp
+1
-1
include/Student.hpp
include/Student.hpp
+1
-1
include/logging.hpp
include/logging.hpp
+82
-0
src/CMakeLists.txt
src/CMakeLists.txt
+1
-1
src/Human.cpp
src/Human.cpp
+4
-2
src/Student.cpp
src/Student.cpp
+5
-3
src/logging.cpp
src/logging.cpp
+42
-0
src/main.cpp
src/main.cpp
+5
-1
No files found.
include/Human.hpp
View file @
093a32f0
...
...
@@ -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
;
};
include/Student.hpp
View file @
093a32f0
...
...
@@ -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
include/logging.hpp
0 → 100644
View file @
093a32f0
#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
src/CMakeLists.txt
View file @
093a32f0
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
src/Human.cpp
View file @
093a32f0
...
...
@@ -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
src/Student.cpp
View file @
093a32f0
#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
src/logging.cpp
0 → 100644
View file @
093a32f0
#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
);
}
src/main.cpp
View file @
093a32f0
#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
();
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment