Logging functionality

I'm making a logging class that is supposed to help with debugging some programs that I'll eventually write in the future.
I was wondering how I would be able to call this logging class once so that it can analyze each statement to see if there was any error, without having to manually call the class and the function after each statement made.

<no code available>
Last edited on
I don't get it. If this is for debugging then why not use the debugger itself? What exactly is the class supposed to do? "help with debugging" is too brief.

I can't think how a class would be able to debug a program.
[How can I] call this logging class once so that it can analyze each statement to see if there was any error, without having to manually call the class and the function after each statement made.

Firstly, I don't understand what you're asking: there's not enough context to give a meaningful response.

Secondly, this looks like an XY problem ( see: http://xyproblem.info/ ): what problem are you trying to solve?
In other words, what is your use case: why do you want this?

I'm making a logging class that is supposed to help with debugging some programs that I'll eventually write in the future.

How can an effective solution be designed for a hypothetical problem?

The risk is that the solution will
a.) solve problems that never occur;
b.) don't solve problems that actually occur; and
c.) not be worth the cost of implementing.

(The suggestion is that you need to understand the problem before solving it. If the problem isn't understood already, it may be in your best interests to wait until there is a case to study.)
Last edited on
yes, it's for my compiler construction class. I'm not sure why either, but that's the assignment :) we're supposed to make a library of logging functions. he said something about making a class with it, so i did
@mbozzi unfortunately, I can't really give any more answers either. that's just how the assignment was given to us. despite asking as many questions as we could about the assignment, the professor continued to not give us too many reasons for the assignment. not only myself, but the entire class is still very confused about it as well.

I can only confidently say that we're supposed to make a library of logging functions that must include the error description when an error is encountered and write it into a text file.

Last edited on
> yes, it's for my compiler construction class.
Some ideas from GCC then.
https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.html#Instrumentation-Options

> I'm making a logging class that is supposed to help with debugging some programs that I'll eventually write in the future.
> I was wondering how I would be able to call this logging class once so that it can analyze each statement to see if there was any error
Wait, is this "error detection" at compile-time (within your as yet to be written compiler)?
@salem c yes, the error detection is at compile time
It's not practical to write a class that detects compiler errors (that would be your entire compiler). What I think you should be writing is a class that is responsible for formatting errors detected by your compiler. This will give all the errors reported by your compiler a uniform appearance and behavior.

Some things your error class should know:
- The current source file (if it supports includes)
- The current line number in the source file
- The current column number in the source file. This advances as you parse the current line.
- The contents of the current line
- Whether or not the current line has already been printed. i.e. If you detect multiple errors in a line, you don't want to print that line multiple times.
- Whether or not the source listing is enabled.
- Maximum number of errors allowed in a compile
- Count of errors encountered so far
- Depending on the nature of your compiler, you might want to differentiate errors and warnings.

1
2
3
4
5
6
7
8
9
10
11
12
13
class ErrorStream
{  std::string  source_file;
    std::string  source_line;
    int line_number;
    int column_number;
    bool line_printed;
    bool listing_enabled;
    int max_errors;
    int error_count;
public:
    ErrorStream();
    void ReportError(int errnum, const std::string & errtext);
};
Last edited on
@AbstractionAnon oh thank you! We've only had a two lectures so far and none of the lectures included anything like formatting errors that would be detected by the compiler.

I'm really unfamiliar with how one would detect errors from the compiler, how does that work? Would you mind providing an example of one and a short description of how it works should really help me learn how to do this. Or maybe a link with one? Thank you :)
Last edited on
Here are some thoughts. We have various logging libraries at work.

Your log messages could have various levels, ranging from "the bare minimum" to "log everything you can think of". We generally use 5 levels at work. You could also do classifications, such as TRACE (prints entry and exit to functions/methods), ERROR (prints errors as they occur), WARNING, etc.

When you print a line, include the source file name and line number. You might have to include both the name/number of our compiler and the name/number of the file being compiled.

If it's going to a file, consider whether you should include a timestamp.

I find that it's very useful to print the name of the current function and the value of the parameters.

Make sure that the log can be aimed at different places: a file, cerr, whatever.

It can be handy to derive the log object from ostream so callers can use all the familiar stream operations. To set the logging level of a message, I added a () operator. So you might log a line like this:
log(2) << "something went wrong in foo(" << param << ")\n";
log(2) calls something like:
Log & Log::operator()(unsigned n) { level = n; return *this}
And when the Log object sees a newline is checks the value of member level against the global current desired level to decide whether to print the message.

Hope this helps,
Dave
I'm really unfamiliar with how one would detect errors from the compiler,

How you detect errors in your compiler really depends on the language you're trying to parse.

Some errors are obvious, like "Could not open input file" However most of the errors you are going to detect revolve around parsing your input file.

Parsing is the process of taking one or more tokens from the source file and reducing them in what is called a "production". If your tokens don't reduce to a production, then you have a syntax error in the source file you're parsing.

Let's say you paring a simple declaration statement such as int a; You want to make sure you recognize the type as a valid type. You want to make sure a is not already defined. You want to make sure the ; is present to terminate the statement. Those are three simple checks that you compiler will make. Each of those checks should result in a error message if the check fails.

I don't know if your instructor has introduced you to Backus-Naur Form (BNF).
https://en.wikipedia.org/wiki/Backus%E2%80%93Naur_form
Describing your syntax in BNF is a great way to understand how to parse it.
BNF is also the basis of a number of compiler building tools such as YACC.
https://en.wikipedia.org/wiki/Yacc

BTW, dhayden's suggestion of an error level is a very good one and something I overlooked in my previous post.
Last edited on
Topic archived. No new replies allowed.