5 Replies Latest reply on Aug 30, 2016 1:56 PM by Whatevermajorloser

    Logging user activity

    Whatevermajorloser Level 1

      I've got a script on a server that 40+ people access and use through an action in Photoshop. It does a number of checks/corrections to the users open images before closing and uploading them to our website. It also has built in logging through a function that writes to several '.log' files on the server based on what corrections have been made to the current image. There's also a general log that logs what images were processed, by which user and at which time, all written to the same 'Uploaded.log' file.

       

      This is a snippet of the log function, which demonstrates its essence:

       

      // If log file already exists
      if (logFile.exists) {
      
          // Read log
          logFile.open("r");
          var logDataString = logFile.read();
          logFile.close();
      
          // Write saved and new data to file
          logFile.open("w");
          logFile.write(logDataString);
          logFile.writeln(timeAndDate + " - " + logData + " - " + USER_NAME);
          logFile.close();
      
          // If file doesn't exist, create and write new data  
      } else {
          logFile.open("w");
          logFile.writeln(timeAndDate + " - " + logData + " - " + USER_NAME);
          logFile.close();
      }
      

       

      What the function essentially does is read the log file by pulling it into a variable, appends whatever activity the user has done, and then re-writes the log file to the server, overwriting the old one. However, what happens fairly often due to the amount of users writing to it at the same time is one user might be in the middle of overwriting the file while another is reading, so a big chunk of it can get lost in this overlap.

       

      Could any of you recommend a way to avoid this? I was thinking of having the function create a temp file before it starts logging and then deletes when it's finished. That way, I could have it check for this file and only start logging once it's been deleted by another running the script. So, a simple while loop like this:

       

      while(tempFile.exists){
          // do nothing
      }
      

       

      Would this be an effective solution?