Dynamically Opening Files for Output
-
I have a pretty simple logger COM component that I am building right now that simply logs entries to a text file when the "LogEntry" call is made. However currently it only logs to one file and that file is declared at construction. I am now trying to add both a "public" and "private" functionality. First, I need to check a conditional that the "LogEntry" is either public or private. If it is public, I do everything the same as I do now, if it is private, I need to log it to it's private file. I am trying to find a way to dynamically enter into a file. I have tried this already: I declared a std::map object. The string is the FileName. I decided to use a map to make sure I open the file the first time I log to the private file. I run a .find on the string to see if the file is there. If the file is there, I log to it. If it is not there, I open the file and log to it, leaving it open for future logging. None of this is working for me. Since there will be many different clients using this logger component, I need a way to determine which file to log to as they will all have separate files. Then log to that file. But since I don't know how many clients I will have, I need this to be dynamic. Any help will be appreciated. Thank you.
-
I have a pretty simple logger COM component that I am building right now that simply logs entries to a text file when the "LogEntry" call is made. However currently it only logs to one file and that file is declared at construction. I am now trying to add both a "public" and "private" functionality. First, I need to check a conditional that the "LogEntry" is either public or private. If it is public, I do everything the same as I do now, if it is private, I need to log it to it's private file. I am trying to find a way to dynamically enter into a file. I have tried this already: I declared a std::map object. The string is the FileName. I decided to use a map to make sure I open the file the first time I log to the private file. I run a .find on the string to see if the file is there. If the file is there, I log to it. If it is not there, I open the file and log to it, leaving it open for future logging. None of this is working for me. Since there will be many different clients using this logger component, I need a way to determine which file to log to as they will all have separate files. Then log to that file. But since I don't know how many clients I will have, I need this to be dynamic. Any help will be appreciated. Thank you.
If there is a separate instance of your COM object for each client, then make the handle to the log file a private member of each COM object instance. You will then log to the correct file for each client. If you also open the log file for exclusive write access, then no other client will be able to open the same log file for writing. Your public file's sharing should be modified to allow multiple clients to open filehandles and write to it. You should protect the writing process with a mutex or some similar synchronization object so that they don't all actually ever write to it at the same time.
-
If there is a separate instance of your COM object for each client, then make the handle to the log file a private member of each COM object instance. You will then log to the correct file for each client. If you also open the log file for exclusive write access, then no other client will be able to open the same log file for writing. Your public file's sharing should be modified to allow multiple clients to open filehandles and write to it. You should protect the writing process with a mutex or some similar synchronization object so that they don't all actually ever write to it at the same time.
Also , what is the Apartment Model ? perhaps you can change that and then try using the handle to log file as private member. Regards Tarundeep Singh Kalra Blue Pill or Red Pill........??
-
If there is a separate instance of your COM object for each client, then make the handle to the log file a private member of each COM object instance. You will then log to the correct file for each client. If you also open the log file for exclusive write access, then no other client will be able to open the same log file for writing. Your public file's sharing should be modified to allow multiple clients to open filehandles and write to it. You should protect the writing process with a mutex or some similar synchronization object so that they don't all actually ever write to it at the same time.
My COM object is a singleton. Only one instance. The Public writing is fine as I already had that working properly. The Private is the functionality I am adding. Basically, if BOB calls LogEntry(private) then it needs to open BOB.txt on the first instance and log to BOB.txt every time after that. If TOM calls LogEntry(private) then it needs to open TOM.txt on the first instance and log to TOM.txt every time after that. ...and so on.