lllogchat.cpp

Go to the documentation of this file.
00001 
00032 #include "llviewerprecompiledheaders.h"
00033 
00034 #include "lllogchat.h"
00035 #include "viewer.h"
00036         
00037 const S32 LOG_RECALL_SIZE = 2048;
00038 
00039 //static
00040 LLString LLLogChat::makeLogFileName(LLString filename)
00041 {
00042         filename = gDirUtilp->getExpandedFilename(LL_PATH_PER_ACCOUNT_CHAT_LOGS,filename.c_str());
00043         filename += ".txt";
00044         return filename;
00045 }
00046 
00047 LLString LLLogChat::timestamp(bool withdate)
00048 {
00049         U32 utc_time;
00050         utc_time = time_corrected();
00051 
00052         // There's only one internal tm buffer.
00053         struct tm* timep;
00054 
00055         // Convert to Pacific, based on server's opinion of whether
00056         // it's daylight savings time there.
00057         timep = utc_to_pacific_time(utc_time, gPacificDaylightTime);
00058 
00059         LLString text;
00060         if (withdate)
00061                 text = llformat("[%d/%02d/%02d %d:%02d]  ", (timep->tm_year-100)+2000, timep->tm_mon+1, timep->tm_mday, timep->tm_hour, timep->tm_min);
00062         else
00063                 text = llformat("[%d:%02d]  ", timep->tm_hour, timep->tm_min);
00064 
00065         return text;
00066 }
00067 
00068 
00069 //static
00070 void LLLogChat::saveHistory(LLString filename, LLString line)
00071 {
00072         if(!filename.size())
00073         {
00074                 llinfos << "Filename is Empty!" << llendl;
00075                 return;
00076         }
00077 
00078         FILE* fp = LLFile::fopen(LLLogChat::makeLogFileName(filename).c_str(), "a");            /*Flawfinder: ignore*/
00079         if (!fp)
00080         {
00081                 llinfos << "Couldn't open chat history log!" << llendl;
00082         }
00083         else
00084         {
00085                 fprintf(fp, "%s\n", line.c_str());
00086                 
00087                 fclose (fp);
00088         }
00089 }
00090 
00091 void LLLogChat::loadHistory(LLString filename , void (*callback)(LLString,void*), void* userdata)
00092 {
00093         if(!filename.size())
00094         {
00095                 llerrs << "Filename is Empty!" << llendl;
00096         }
00097 
00098         FILE* fptr = LLFile::fopen(makeLogFileName(filename).c_str(), "r");             /*Flawfinder: ignore*/
00099         if (!fptr)
00100         {
00101                 return;                 //No previous conversation with this name.
00102         }
00103         else
00104         {
00105                 char buffer[LOG_RECALL_SIZE];           /*Flawfinder: ignore*/
00106                 char *bptr;
00107                 S32 len;
00108                 bool firstline=TRUE;
00109 
00110                 if ( fseek(fptr, (LOG_RECALL_SIZE - 1) * -1  , SEEK_END) )              
00111                 {       //File is smaller than recall size.  Get it all.
00112                         firstline = FALSE;
00113                         if ( fseek(fptr, 0, SEEK_SET) )
00114                         {
00115                                 fclose(fptr);
00116                                 return;
00117                         }
00118                 }
00119 
00120                 while ( fgets(buffer, LOG_RECALL_SIZE, fptr)  && !feof(fptr) ) 
00121                 {
00122                         len = strlen(buffer) - 1;               /*Flawfinder: ignore*/
00123                         for ( bptr = (buffer + len); (*bptr == '\n' || *bptr == '\r') && bptr>buffer; bptr--)   *bptr='\0';
00124                         
00125                         if (!firstline)
00126                         {
00127                                 callback(buffer,userdata);
00128                         }
00129                         else
00130                         {
00131                                 firstline = FALSE;
00132                         }
00133                 }
00134                 callback("-- End of Log ---",userdata);
00135                 
00136                 fclose(fptr);
00137         }
00138 }

Generated on Thu Jul 1 06:08:49 2010 for Second Life Viewer by  doxygen 1.4.7