|
| ikantspelwurdz (7) | |||
| I'm trying to make a utility that will log all power messages (and eventually do some other cool stuff, but for now I'm focusing on that). However, I'm painfully green when it comes to WinAPI, and although I've been reading up on it all month, I'm still not confident I understand what's going on in what I've done so far. Much of what I've done is more or less copied and pasted from tutorials that I only understand in a rudimentary sense. My code has a callback function "WndProc" with a case switch for types of the message WM_POWERBROADCAST. Currently, for each case, it will create a new log file and write data into that log. But what I want to do is make it append an existing log. Is there some way to feed this WndProc function a local ofstream pointer? I could probably just declare this ofstream globally, but I think that's asking for multithreading-related trouble. Is there a better way to accomplish what I'm trying to do? Also, if there are any miscellaneous bone-headed moves I'm making, especially ones related to WinAPI stuff, I would greatly appreciate being informed of them. My code:
| |||
| writetonsharma (832) | |
| Instead of creating the filename at runtime, give it a hardcoded name and then keep on appending text to it by opening it in ios::app mode. Otherwise, you can create a static file stream object in winproc and use it again and again without changing it and then can close it WM_CLOSE or WM_DESTROY. | |
| freddie1 (108) | |
| Hi ikantspelwurds, I'd have liked to have run your program, but couldn't get it to compile. Piles of errors. Does yours compile? What development environment/compiler you using? I tried on Dev-C++. I also have CodeBlocks, VC6 and VC9, but I didn't try those. I'm fairly experienced at Win32, but never fooled with power messages. That's why it interested me. | |
| ikantspelwurdz (7) | ||||
The program needs to be capable of writing a new log file whenever the current one gets too big. Naming it after the current time seemed like the simplest way to ensure no two files have the same name.
Is that thread-safe? If so, how do you do that? I'm not really sure what I'm doing in winproc - I mainly just screwed around with the function until I got something that appeared to work.
It compiles fine and runs as described. I'm using VC9 Standard, and compiling using the Windows 7 SDK. As far as I know, VC9 express should work too. | ||||
Last edited on | ||||
| writetonsharma (832) | |||||
| for file name, use a naming convention and append a time-stamp with it. this will free you from name clashing. when the file gets big, make a new file with new time-stamp. If your application is multi-threaded, you need to sync the write to the file. Otherwise, you have written a separate function for writing to the file. just change it to pass only the string. open the stream inside the function only. like this:
Save the FileName in your winproc and keep changing it as soon you create a new file. Dont open file stream at every place in the program.
| |||||
Last edited on | |||||
This topic is archived - New replies not allowed.
