Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to separate logging logic from business logic in a C program? And in a C++ one?

I am currently coding in C and I have lots of printfs so that I can track, at some times, the flow of my application. The problem is that some times I want more detail than others, so I usually spend my time commenting/uncommenting my C code, so I can get the appropriate output.

When using Java or C#, I can generally separate both my implementation code from the logging logic by using Aspects.

Is there any similar technique you use in C to get around this problem?

I know I could put a flag called DEBUG that could be either on or off, so I wouldn't have to go all around and comment/uncomment my whole code every time I want to either show or hide the printfs. The question is I'd like to also get rid of the logging logic in my code.

If instead of C I was coding in C++, would it be any better?

Edit

It seems there is an AspectC++, so for C++ there seems to be a solution. What about C?

Thanks

like image 420
devoured elysium Avatar asked Nov 14 '10 16:11

devoured elysium


People also ask

What is the difference between business logic and application logic?

Business logic refers to the rules and procedures that govern a business, including things like pricing, discounts, inventory levels, customer eligibility, etc. Application logic, on the other hand, is the code that implements those business rules within a specific application.

What is the business logic layer?

The business logic layer is the business components that provide OAGIS services to return data or start business processes. The presentation layer uses these OAGIS services to display data, or to invoke a business process. The business logic provides data required by the presentation layer.

What is business logic in Java with example?

Business logic is any Java™ code that is invoked as an action when an event occurs, such as a host screen being recognized or your HATS application being started. Business logic is specific to the application and is not provided as part of HATS.


1 Answers

IME you cannot really separate logging from the algorithms that you want to log about. Placing logging statements strategically takes time and experience. Usually, the code keeps assembling logging statements over its entire lifetime (though it's asymptotic). Usually, the logging evolves with the code. If the algorithm changes often, so will usually the logging code.

What you can do is make logging as unobtrusive as possible. That is, make sure logging statements always are one-liners that do not disrupt reading the algorithm, make it so others can insert additional logging statements into an existing algorithm without having to fully understand your logging lib, etc.

In short, treat logging like you treat string handling: Wrap it in a nice little lib that will be included and used just about everywhere, make that lib fast, and make it easy to use.

like image 153
sbi Avatar answered Oct 05 '22 23:10

sbi