 Okay, now we're going to take what we've learned and we're going to put it into a logger class that we can reuse in other projects To do this we need to start by well, you guessed it making an actual class and let's call this The ever descriptive logger. Why not? Now we're going to do logger a little bit different First thing we're going to do is add some includes Of course, we're going to need Q debug for this We also need Q file Q date time and Q dir Along with iostream All right now that we have our class we're going to make some static items in here Darn you case sensitivity you strike again All right, so we're going to Make a function now called attach This will allow us to attach the message handler to the static Parts of this class when I say static parts. It's because we're going to do 99% of this statically so we don't have to make an instance of it Makes it much much easier to work with And I'm just going to copy and paste in our handler here. Notice how it has the same signature let's go ahead and Add these definitions in right now because we're working statically We need to set these variables and we cannot do this in the constructor So this may look a little weird, but this is how you would initialize a static variable So I set the file name and we want this to be q dir And we're just going to set current path Maybe help if I didn't have that in quotes. There we go separator and There we go. We're just going to call this log dot txt Now we want to set our logging value. So we're going to say logger logging And we're going to set this to false initially Because we don't want to start logging until well, you guessed it. We've actually attached the logger And we are going to make our q default message handler It's going to copy and paste that from the previous project Save us a bit of typing here So once again, all this does is it calls q q install message handler with a null pointer So it gets just the default handle that q uses. All right now for attach This is where we're going to start actually logging We're going to install the message handler and we're just going to give it our function That's just going to point it right to this guy whoops actually right to this guy right here That's actually the constructor boy. That was a bungal And then we're going to say logger logging equal true So we're just going to set that static variable. So really all we're doing is we're Actually, let's just in case We're going to set logging to true and then we're going to install the message handler and at that point messages will start flowing into our handler here now We've got a bit of typing So what we're going to do here is we're going to say if logger logging Then we're going to do something Now regardless of whether or not we actually process anything We want to pass this back to the default message handler, which of course we get up here So if you skip the last video really what's going on is we get the default message handler Which in this case just prints it out into the standard output And we're going to say hey install our handler this guy But after we've handled it, we need to pass it back to the default message handler or nothing's going to appear on the screen It just goes into this black hole because well, we never put it back all right I'm going to save a bit of typing here through the magic of copy and paste because we already know what this does So we've just got our switch along with a q string See if we can beautify this a little bit Refractor it's not let me do it auto indent. There it is That looks a little bit better and Add q-text stream in here. I always forget to add that for some reason and We need our file Let's call this q file file and it's logger I'm sorry logging file name Actually, this is logger got a little ahead of myself there Get our static name there all right, we're going to go ahead and Actually crank open this file here And if we're able to open that that's when we're going to actually process this here we go And we've done this before so i'm just going to grab the code Copy and paste that in bang The only real difference is we're using endel from standard output or i'm sorry, uh ios stream Some people like using endel some people don't i don't think it really matters either way I'm sure there's some complexity to it, but i've never really run into anything major and then we're just going to flush this out and Close the file So looking at this code. It's really not a whole lot different. We've got the handler in here We're just simply going to attach by setting a boolean value and then installing the message handler to our function Let's look at our main code here. See if we can actually get this thing to work So we're going to add qtabug. We're going to add our logger Grab our file name out of there We want to go ahead and attach this so we're going to say logger Attach Once we've done that the message handler should be installed. So we're going to actually test that We're going to turn logging off oops Just by setting that to false And through the magic copy and paste we can of course turn it back on So really this just simply works because we're saying If we are logging So because we're setting that to false, we're just going to skip right over this and let's just say finished Let's save this Go ahead and run it. All right. So we can see there's our file We've done a test don't log this even though it's displayed in the console It's displayed simply because We look at this That is if we're logging but because we're not logging it just jumps right to the default message handler Which of course just spits it right back out into the console for us and then finished So let's go check this log file. Let's crack open our directory. There's our log And you can see test and finished We didn't have the do not log simply because we turned logging off So everything's working as expected now. This is the really cool feature of this Notice how All of this happens outside of our logger class We can actually add another class in here. Let's add a test Make this a queue object And we're just going to add a test in here Add queue to bug and you guessed it really all this thing's going to do is we're going to Do a queue warning Constructor cannot have a return type. Oh Probably because we named this test. Let's name this something else like testing Because we did actually just make the constructor an accident. There we go Silly me. There we go And we could you know actually just why not let's build our constructor in there too Let's go in here and let's include our test class. Let's call our function Let's save and let's run this You can see how we have our file. We have tests do not log this. This will not appear in our log But then we're also constructing an object Calling a function in that object and we're finished Let's go ahead and pull up our log and let's reload this. Bang. Sure enough. We've got everything Including the constructor As you can see, this is insanely cool. You can do some really awesome things with very minimal effort And that's what's programming is really all about is just making cool things happen with expending as little effort as possible I hope you enjoyed this video. It's part of a video series I have out on udemy.com This particular video is a follow-on in the intermediate course, but you can start in cute core for beginners And if we just kind of crack this open It's got a lot of content that'll take you from an absolute beginner that just knows absolutely nothing from hello world all the way up to What are templates generics air handling in classes? This specific video is part of the cute core intermediate class This picks right up where the beginners course left off and we do things like memory management collections Working with settings the file system a lot of file system Compression serialization and much much more