lllogchat.cpp

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

Generated on Fri May 16 08:33:46 2008 for SecondLife by  doxygen 1.5.5