Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Dynamically Opening Files for Output

Dynamically Opening Files for Output

Scheduled Pinned Locked Moved C / C++ / MFC
comhelp
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    ChemmieBro
    wrote on last edited by
    #1

    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.

    B 1 Reply Last reply
    0
    • C ChemmieBro

      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.

      B Offline
      B Offline
      Blake Miller
      wrote on last edited by
      #2

      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.

      T C 2 Replies Last reply
      0
      • B Blake Miller

        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.

        T Offline
        T Offline
        Tarundeep Singh Kalra
        wrote on last edited by
        #3

        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........??

        1 Reply Last reply
        0
        • B Blake Miller

          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.

          C Offline
          C Offline
          ChemmieBro
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups