• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
TTT_Dutch

How to Log and stay Modular

56 posts in this topic

Well in my team's game, we have a logging system setup. There is a Log class that allows you to write to a log by just specifying a file in the constructor then using the WriteToLog call.
Example:

Log EngineLog("Engine.log");
EngineLog.WriteToLog("Failed to load...");

Now, all over the code I have included a file that declares a global log that is used by the engine. This global log is used throughout the engine and really messes up my goal of keeping things modular. Now, what is the best way to log but still keep things modular? Please provide more than just, "Use cout or printf".

Thanks,
Brent
0

Share this post


Link to post
Share on other sites
[quote name='TTT_Dutch' timestamp='1314931684' post='4856567']
Now, all over the code I have included a file that declares a global log that is used by the engine. This global log is used throughout the engine and really messes up my goal of keeping things modular. Now, what is the best way to log but still keep things modular? Please provide more than just, "Use cout or printf". [/quote]


A global log is by definition, well, global. So I'm not sure what you're really after. But perhaps rather than calling functions on the global log directly, you could implement a reference mechanism in your log class and do something like this:

[code]
Log EngineLog("Engine.log");
Log GlobalLog(App.Log);
...
EngineLog.WriteToLog("Failed to load...");
GlobalLog.WriteToLog("foo...");[/code]


or, if you want to include data in both global and local logs,

[code]
Log MyLog("Engine.log", App.Log); //localfile_name, global_log_ref
...
MyLog.WriteToLocal("Failed to load...");
MyLog.WriteToGlobal("foo...");
MyLog.WriteToBoth("...bar");[/code]


Not really sure I understand what you're really after, perhaps...
1

Share this post


Link to post
Share on other sites
I am guessing you have an existing logging system implemented as per your snippet that forces you to specify the logfile name every time you want to log something. And you are not comfortable with having to specify this logfile name everywhere in your code.

Well the simplest solution is to just create a global variable for the filename that is initialized/set in only one place (if your code is like mine, you would probably have one or more header files somewhere that declares some global enums, variables, references, etc). And of course you could do stuff like having a startup object read from a config file to populate all these global variables when your game starts up.

Another way is for you to write a wrapper class around the existing Log class. This wrapper will manage (and hide) all the details of what logfile to open, when to open it, when to write, etc. So wherever you need to log a message, just call the constructor with the message and just let autoscoping take care of destroying the object.

[code]LogWrapper EngineLog("this is a log...");[/code]

I have a logger class that collects log messages together in memory until a certain size before opening the logfile and dumping everything in. Works for logs that do not deal with debugging crashes!
1

Share this post


Link to post
Share on other sites
I don't understand this fascination either. It would drive me insane having to create a logging object everywhere I needed to add an entry, or passing a logger reference to all my functions, let alone having to manually enter the filename to write to. In my opinion the single most useful and quickest to use logging method is:

[code]#define Log(msg) SomeFreeLoggingFunction(msg, __FILE__, __LINE__)[/code]

Now go and worry about writing the game instead.
1

Share this post


Link to post
Share on other sites
What I've seen in the past is that Log is a strategy associated with each module. Your 'glue' creates some common log and when the modules are created the glue specifies the common log as the strategy. Easier in not-C++, overkill for most things. As others have said (more or less), worry about functionality before maintainability/reusability; worry about your game not your code.
1

Share this post


Link to post
Share on other sites
My engine is so modular that each module is an entirely different Microsoft® Visual Studio® project, combined into one solution.

The solution you are seeking is quite simple.
One of the modules should act as a foundation for all of the others. In my case, my foundation is the “L. Spiro Standard” library and its job is only to expose features that could be shared across not only the game engine but any project. For example, a CTime class, a StrLen() function, some routines for UTF handling, etc.

If I had a need for a logging function, it would go there.


There is nothing non-modular about this. My model library has no idea what a “terrain library” is, yet both of them are built on top of a few common foundation layers, which themselves also don’t know what models or terrain is.

If you do not have such an organization, you need to make such an organization.


L. Spiro
1

Share this post


Link to post
Share on other sites
I agree with the general sentiment that you are over-engineering, or that you are forcibly applying the principle of EVERYTHING MUST BE AN OBJECT which you may have picked up at college / uni. OOP is a methodology of programming, not an ideology.

When it comes to logging, you have a finite number of options.

1) Create a new logger and then throw it away every time you want to log something
2) have a single global logger. (no, I didn't say singleton, I said single global instance, the terms are not synonymous)
3) use a free function, or depending on the language, a static function.
4) pass a logger to every single constructor of every single object your game uses.

It should be obvious from these choices what the correct course of action is. Let the academics continue to think inside their little boxes up there in that ivory tower while you like, get some work done.
1

Share this post


Link to post
Share on other sites
Okay thank you to all of you for your replies! I think I finally have a grasp on what I should do.

Thanks,
Brent
0

Share this post


Link to post
Share on other sites
[quote name='Katie' timestamp='1315058524' post='4857146']"but globals are bad right?"

No, they're not. They're bad if you're an academic computer scientist. Academic computer scientists never actually have to ship code. Globals work, use what works, ship code.[/quote]

The only reason academics are even aware of the troubles of global variables is because professionals, who were trying to ship products, discovered that global variables were one of the culprits keeping them from doing so, by making their code-bases unstable and unpredictable.

The biggest problem with the idea that "glabals are bad" is that it's so basic and so easy to remember and file away, that sometimes when it comes to implementing something and that old concept pops up a red flag, we might forget to take a moment to ponder the issue again, think about what exactly the problem was, and how exactly it applies to our current situation, if at all.

It would have been bad to let the concept back a code-base into the corner of passing a log object to every single function call. But as it happens, that doesn't mean the concept is wrong, just a little too easy to under-think.
0

Share this post


Link to post
Share on other sites
[quote name='speciesUnknown' timestamp='1315060652' post='4857155']
I agree with the general sentiment that you are over-engineering, or that you are forcibly applying the principle of EVERYTHING MUST BE AN OBJECT which you may have picked up at college / uni. OOP is a methodology of programming, not an ideology.

When it comes to logging, you have a finite number of options.

1) Create a new logger and then throw it away every time you want to log something
2) have a single global logger. (no, I didn't say singleton, I said single global instance, the terms are not synonymous)
3) use a free function, or depending on the language, a static function.
4) pass a logger to every single constructor of every single object your game uses.

It should be obvious from these choices what the correct course of action is. Let the academics continue to think inside their little boxes up there in that ivory tower while you like, get some work done.
[/quote]

Option 4 with IoC wiring app/domain services is best.
0

Share this post


Link to post
Share on other sites
[quote name='Katie' timestamp='1315058524' post='4857146']
" but globals are bad right?"

No, they're not. They're bad if you're an academic computer scientist. Academic computer scientists never actually have to ship code. Globals work, use what works, ship code.

"really messes up my goal of keeping things modular."

Don't have a goal of being modular. Have a goal of "shipping code". What value to you is "being modular"? None. It's an **ideal**. It's not a goal. Your goal is to ship code. That means you get to sacrifice ideals sometimes. This could be one of those times.

You're over-thinking this problem. Everyone always does. Everyone thinks there's a problem with "logging" because it's always either too verbose or not verbose enough and it's never quite right and everyone thinks the way to fix it is to somehow make the logging production more complication.

The problem isn't with the logging. It's with the tool on the other end. Make your program be either "verbose" or "succinct" based on a flag. That's all you need on that front.

Then just output strings but have smarter tools looking at the output.

The UNIX world manages to handle tons and tons of logging using "syslog" which is hardly sophisticated. "syslogd", the program on the other end, that has versions which'll do everything up to handling global networks with millions of servers...

Don't try and solve your logging analysis problems during your log generation.
[/quote]

While of course shipping code is the ultimate goal, it shouldn't be the principle you use to override other design considerations either. Modularity isn't just a valueless ideal, it helps in both designing the infrastructure of the system and in maintaining the system when bugs start to creep in. If your design isn't modular, then when you want to fix issues with rendering, for example, you need to be aware of how your collision detection works, and what game logic is affected and a hundred other things that you might have linked there because it 'worked', at the time, and it meant you got to ship that much faster. The result of ignoring modularity to ship code is that your code base gets spaghettified, which accrues ever increasing costs for maintenance and reuse later on.

You can go overboard, of course, with the issue of logging for example, but you shouldn't rail against programming ideals just because in the trenches you can't always see the value to spending more time in the design.
1

Share this post


Link to post
Share on other sites
[quote name='VReality' timestamp='1315125196' post='4857406']
The biggest problem with the idea that "glabals are bad" is that it's so basic and so easy to remember and file away, that sometimes when it comes to implementing something and that old concept pops up a red flag, we might forget to take a moment to ponder the issue again, think about what exactly the problem was, and how exactly it applies to our current situation, if at all.[/quote]

You need to fill a balloon or a blimp. Do you use hydrogen or helium? Helium.
Air cooled or water cooled engine? Water cooled.
4 stroke or 2 stroke engine? 4 stroke.

But, but, but... Each of the alternatives has advantages and tech has advanced and we can make it work and we've learned a lot and .....

This falls under engineering. You can always find an exception, there are always people who actually do know better (not just think they do), there might be a better choice. But it's rare to be working on a project that can benefit from those with people who are even interested into dealing with such tasks or where such gambles will have positive outcome.

Use the proven approach using methodology that was learned on hard failures on actual real world circumstances. Across thousands of projects, some methods are marginally better. And while 2% advantage on one single concept won't matter, combining different "better" ways will: 0.48*0.48*0.48*...* and the chance of failure is drastically affected.

Simple example - why do people claim globals/singletons are bad, then insist on using IoC/DI framework which commonly starts with singletons, yet doesn't exhibit any of mentioned problems? Because it's not globals/singletons, but most people involved in such discussions haven't been able to understand the true issues caused. So just don't use them.
2

Share this post


Link to post
Share on other sites
But nothing.

This isn't a case of finding the exception to the rule, at all. It's a case of bothering to remember what that rule actually was in the first place.

"Money is the root of all evil", right? No. "The [i][b]love [/b][/i]of money is the root of [b][i]all kinds of[/i][/b] evil." Misquotes that are missing important nuance render reasonable ideas ridiculous.

It's easy enough to remember "gloabls are bad", but is there really much value in misremembering a principle and applying it incorrectly? Global [i][b]variables[/b][/i] cause problems. And they cause problems for a [i][b]reason[/b][/i]. Maybe remembering the 'misquote' is better than forgetting the issue altogether, but better yet is to take a second to understand the issue, so we can think about how the reasons apply to our situation.

"Just don't use 'em" is exactly the sort of thing that will have you trying to pass a log object to every function call in your codebase.

Yeah, there will be a lot of people who want to question various practices, and who might do so poorly because they don't understand the underlying reasoning (myself not excluded). But that's why we have colleagues and technical forums.
1

Share this post


Link to post
Share on other sites
[quote name='return0' timestamp='1315135230' post='4857439']
[quote name='speciesUnknown' timestamp='1315060652' post='4857155']
I agree with the general sentiment that you are over-engineering, or that you are forcibly applying the principle of EVERYTHING MUST BE AN OBJECT which you may have picked up at college / uni. OOP is a methodology of programming, not an ideology.

When it comes to logging, you have a finite number of options.

1) Create a new logger and then throw it away every time you want to log something
2) have a single global logger. (no, I didn't say singleton, I said single global instance, the terms are not synonymous)
3) use a free function, or depending on the language, a static function.
4) pass a logger to every single constructor of every single object your game uses.

It should be obvious from these choices what the correct course of action is. Let the academics continue to think inside their little boxes up there in that ivory tower while you like, get some work done.
[/quote]

Option 4 with IoC wiring app/domain services is best.
[/quote]


Okay, I will talk with my team and see how we can implement that.

BTW thank you to everyone for all your wonderful answers. Even if I don't quote them, it doesn't mean I didn't learn a lot from them.

-Brent
0

Share this post


Link to post
Share on other sites
Along the lines of what return0 posted, I use a combination of the "Service Locator" pattern and "Dependency Injection".

EDIT: I don't log in any time-critical situations unless debugging, so the overhead of any service location is negligible.
0

Share this post


Link to post
Share on other sites
[quote name='TTT_Dutch' timestamp='1314931684' post='4856567']
Now, all over the code I have included a file that declares a global log that is used by the engine. This global log is used throughout the engine and really messes up my goal of keeping things modular. Now, what is the best way to log but still keep things modular? Please provide more than just, "Use cout or printf".
[/quote]
I really have to stop and say "why not use std::clog or printf?"

I frequently hear "oh yes, the wonderful solution that does exactly what I need already exists, but it's not invented here" but I also see a lot of [url="http://http://upload.wikimedia.org/wikipedia/commons/2/20/Rouleaux_triangle_Animation.gif"]reinvented wheels[/url].
0

Share this post


Link to post
Share on other sites
[quote name='TTT_Dutch' timestamp='1315361101' post='4858431']
[quote name='return0' timestamp='1315135230' post='4857439']
[quote name='speciesUnknown' timestamp='1315060652' post='4857155']
I agree with the general sentiment that you are over-engineering, or that you are forcibly applying the principle of EVERYTHING MUST BE AN OBJECT which you may have picked up at college / uni. OOP is a methodology of programming, not an ideology.

When it comes to logging, you have a finite number of options.

1) Create a new logger and then throw it away every time you want to log something
2) have a single global logger. (no, I didn't say singleton, I said single global instance, the terms are not synonymous)
3) use a free function, or depending on the language, a static function.
4) pass a logger to every single constructor of every single object your game uses.

It should be obvious from these choices what the correct course of action is. Let the academics continue to think inside their little boxes up there in that ivory tower while you like, get some work done.
[/quote]

Option 4 with IoC wiring app/domain services is best.
[/quote]


Okay, I will talk with my team and see how we can implement that.

BTW thank you to everyone for all your wonderful answers. Even if I don't quote them, it doesn't mean I didn't learn a lot from them.

-Brent
[/quote]

Holy shit man, option 4 was supposed to be an obviously wrong answer, and return0's addition was a joke O_o please tell me that from all this you didn't learn that the best solution was the most complicated and intrusive one?
2

Share this post


Link to post
Share on other sites
I want to think he was joking too. So much that I've convinced myself. The world is a much nicer place that way.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0