Track log names in a json map AND Fix processing local log load as system

Update old style callbacks for chatFromLogFile functions to lambdas
Keep track of people's log's name using their IDs, but don't rename.
Fallback on ID if name is empty (if the grid is a meanie)
Also track group names this way, sometimes those change.
Prefer std::array, since we're in the area.
This commit is contained in:
Liru Færs
2020-04-18 00:10:32 -04:00
parent af3c66ad08
commit c201ac90ba
10 changed files with 139 additions and 103 deletions

View File

@@ -262,6 +262,8 @@ LLCacheName::~LLCacheName()
delete &impl;
}
const ReverseCache& LLCacheName::getReverseMap() const { return impl.mReverseCache; }
LLCacheName::Impl::Impl(LLMessageSystem* msg)
: mMsg(msg), mUpstreamHost(LLHost::invalid)
{

View File

@@ -57,6 +57,8 @@ public:
LLCacheName(LLMessageSystem* msg, const LLHost& upstream_host);
~LLCacheName();
const std::map<std::string, LLUUID>& getReverseMap() const;
// registers the upstream host
// for viewers, this is the currently connected simulator
// for simulators, this is the data server

View File

@@ -79,7 +79,7 @@
//
LLColor4 agent_chat_color(const LLUUID& id, const std::string&, bool local_chat = true);
LLColor4 get_text_color(const LLChat& chat, bool from_im = false);
void show_log_browser(const std::string&, const std::string&);
void show_log_browser(const std::string&, const LLUUID&);
//
// Member Functions
@@ -96,7 +96,7 @@ LLFloaterChat::LLFloaterChat(const LLSD& seed)
LLTextEditor* history_editor_with_mute = getChild<LLTextEditor>("Chat History Editor with mute");
getChild<LLUICtrl>("show mutes")->setCommitCallback(boost::bind(&LLFloaterChat::onClickToggleShowMute, this, _2, getChild<LLTextEditor>("Chat History Editor"), history_editor_with_mute));
getChild<LLUICtrl>("chat_history_open")->setCommitCallback(boost::bind(show_log_browser, "chat", "chat"));
getChild<LLUICtrl>("chat_history_open")->setCommitCallback(boost::bind(show_log_browser, "chat", LLUUID::null));
}
LLFloaterChat::~LLFloaterChat()
@@ -243,7 +243,7 @@ void log_chat_text(const LLChat& chat)
else
histstr = chat.mText;
LLLogChat::saveHistory(std::string("chat"), histstr);
LLLogChat::saveHistory("chat", LLUUID::null, histstr);
}
// static
void LLFloaterChat::addChatHistory(LLChat& chat, bool log_to_file)
@@ -535,28 +535,19 @@ LLColor4 get_text_color(const LLChat& chat, bool from_im)
//static
void LLFloaterChat::loadHistory()
{
LLLogChat::loadHistory("chat", &chatFromLogFile, (void*)LLFloaterChat::getInstance());
LLLogChat::loadHistory("chat", LLUUID::null, boost::bind(&LLFloaterChat::chatFromLogFile, getInstance(), _1, _2));
}
//static
void LLFloaterChat::chatFromLogFile(LLLogChat::ELogLineType type, std::string line, void* userdata)
void LLFloaterChat::chatFromLogFile(LLLogChat::ELogLineType type, const std::string& line)
{
switch (type)
bool log_line = type == LLLogChat::LOG_LINE;
if (log_line || gSavedPerAccountSettings.getBOOL("LogChat"))
{
case LLLogChat::LOG_EMPTY:
if (gSavedPerAccountSettings.getBOOL("LogChat"))
addChatHistory(static_cast<LLFloaterChat*>(userdata)->getString("IM_logging_string"), false);
break;
case LLLogChat::LOG_END:
if (gSavedPerAccountSettings.getBOOL("LogChat"))
addChatHistory(static_cast<LLFloaterChat*>(userdata)->getString("IM_end_log_string"), false);
break;
case LLLogChat::LOG_LINE:
addChatHistory(line, FALSE);
break;
default:
// nothing
break;
LLStyleSP style(new LLStyle(true, gSavedSettings.getColor4("LogChatColor"), LLStringUtil::null));
const auto text = log_line ? line : getString(type == LLLogChat::LOG_END ? "IM_end_log_string" : "IM_logging_string");
for (const auto& ed_name : { "Chat History Editor", "Chat History Editor with mute" })
getChild<LLTextEditor>(ed_name)->appendText(text, false, true, style, false);
}
}

View File

@@ -73,7 +73,7 @@ public:
static void triggerAlerts(const std::string& text);
void onClickToggleShowMute(bool show_mute, class LLTextEditor* history_editor, LLTextEditor* history_editor_with_mute);
static void chatFromLogFile(LLLogChat::ELogLineType type, std::string line, void* userdata);
void chatFromLogFile(LLLogChat::ELogLineType type, const std::string& line);
static void loadHistory();
static void* createSpeakersPanel(void* data);
static void* createChatPanel(void* data);

View File

@@ -375,9 +375,7 @@ LLFloaterIMPanel::LLFloaterIMPanel(
if ( gSavedPerAccountSettings.getBOOL("LogShowHistory") )
{
LLLogChat::loadHistory(mLogLabel,
&chatFromLogFile,
(void *)this);
LLLogChat::loadHistory(mLogLabel, mSessionType == P2P_SESSION ? mOtherParticipantUUID : mSessionUUID, boost::bind(&LLFloaterIMPanel::chatFromLogFile, this, _1, _2));
}
if ( !mSessionInitialized )
@@ -861,7 +859,7 @@ void LLFloaterIMPanel::addHistoryLine(const std::string &utf8msg, LLColor4 incol
// Floater title contains display name -> bad idea to use that as filename
// mLogLabel, however, is the old legacy name
//LLLogChat::saveHistory(getTitle(),histstr);
LLLogChat::saveHistory(mLogLabel, histstr);
LLLogChat::saveHistory(mLogLabel, mSessionType == P2P_SESSION ? mOtherParticipantUUID : mSessionUUID, histstr);
// [/Ansariel: Display name support]
}
@@ -1179,9 +1177,9 @@ void LLFloaterIMPanel::onFlyoutCommit(LLComboBox* flyout, const LLSD& value)
}
}
void show_log_browser(const std::string& name, const std::string& id)
void show_log_browser(const std::string& name, const LLUUID& id)
{
const std::string file(LLLogChat::makeLogFileName(name));
const std::string file(LLLogChat::makeLogFileName(name, id));
if (!LLFile::isfile(file))
{
make_ui_sound("UISndBadKeystroke");
@@ -1195,7 +1193,7 @@ void show_log_browser(const std::string& name, const std::string& id)
}
LLFloaterWebContent::Params p;
p.url("file:///" + file);
p.id(id);
p.id(id.asString());
p.show_chrome(false);
p.trusted_content(true);
LLFloaterWebContent::showInstance("log", p); // If we passed id instead of "log", there would be no control over how many log browsers opened at once.
@@ -1206,8 +1204,8 @@ void LLFloaterIMPanel::onClickHistory()
if (mOtherParticipantUUID.notNull())
{
// [Ansariel: Display name support]
//show_log_browser(getTitle(), mOtherParticipantUUID.asString());
show_log_browser(mLogLabel, mOtherParticipantUUID.asString());
//show_log_browser(getTitle(), mSessionType == P2P_SESSION ? mOtherParticipantUUID : mSessionUUID);
show_log_browser(mLogLabel, mSessionType == P2P_SESSION ? mOtherParticipantUUID : mSessionUUID);
// [/Ansariel: Display name support]
}
}
@@ -1671,39 +1669,16 @@ void LLFloaterIMPanel::removeTypingIndicator(const LLUUID& from_id)
}
}
//static
void LLFloaterIMPanel::chatFromLogFile(LLLogChat::ELogLineType type, std::string line, void* userdata)
void LLFloaterIMPanel::chatFromLogFile(LLLogChat::ELogLineType type, const std::string& line)
{
LLFloaterIMPanel* self = (LLFloaterIMPanel*)userdata;
std::string message = line;
switch (type)
bool log_line = type == LLLogChat::LOG_LINE;
if (log_line || gSavedPerAccountSettings.getBOOL("LogInstantMessages"))
{
case LLLogChat::LOG_EMPTY:
// add warning log enabled message
if (gSavedPerAccountSettings.getBOOL("LogInstantMessages"))
{
message = LLFloaterChat::getInstance()->getString("IM_logging_string");
}
break;
case LLLogChat::LOG_END:
// add log end message
if (gSavedPerAccountSettings.getBOOL("LogInstantMessages"))
{
message = LLFloaterChat::getInstance()->getString("IM_end_log_string");
}
break;
case LLLogChat::LOG_LINE:
// just add normal lines from file
break;
default:
// nothing
break;
LLStyleSP style(new LLStyle(true, gSavedSettings.getColor4("LogChatColor"), LLStringUtil::null));
mHistoryEditor->appendText(log_line ? line :
getString(type == LLLogChat::LOG_END ? "IM_end_log_string" : "IM_logging_string"),
false, true, style, false);
}
//self->addHistoryLine(line, LLColor4::grey, FALSE);
LLStyleSP style(new LLStyle(true, gSavedSettings.getColor4("LogChatColor"), LLStringUtil::null));
self->mHistoryEditor->appendText(message, false, true, style, false);
}
void LLFloaterIMPanel::showSessionStartError(

View File

@@ -126,7 +126,7 @@ public:
// Handle other participant in the session typing.
void processIMTyping(const LLUUID& from_id, BOOL typing);
static void chatFromLogFile(LLLogChat::ELogLineType type, std::string line, void* userdata);
void chatFromLogFile(LLLogChat::ELogLineType type, const std::string& line);
//show error statuses to the user
void showSessionStartError(const std::string& error_string);

View File

@@ -36,35 +36,100 @@
#include "lllogchat.h"
#include "llappviewer.h"
#include "llfloaterchat.h"
#include "llsdserialize.h"
static std::string get_log_dir_file(const std::string& filename)
{
return gDirUtilp->getExpandedFilename(LL_PATH_PER_ACCOUNT_CHAT_LOGS, filename);
}
//static
std::string LLLogChat::makeLogFileName(std::string filename)
std::string LLLogChat::makeLogFileNameInternal(std::string filename)
{
if (gSavedPerAccountSettings.getBOOL("LogFileNamewithDate"))
static const LLCachedControl<bool> with_date(gSavedPerAccountSettings, "LogFileNamewithDate");
if (with_date)
{
time_t now;
time(&now);
char dbuffer[100]; /* Flawfinder: ignore */
if (filename == "chat")
{
static const LLCachedControl<std::string> local_chat_date_format(gSavedPerAccountSettings, "LogFileLocalChatDateFormat", "-%Y-%m-%d");
strftime(dbuffer, 100, local_chat_date_format().c_str(), localtime(&now));
}
else
{
static const LLCachedControl<std::string> ims_date_format(gSavedPerAccountSettings, "LogFileIMsDateFormat", "-%Y-%m");
strftime(dbuffer, 100, ims_date_format().c_str(), localtime(&now));
}
filename += dbuffer;
std::array<char, 100> dbuffer;
static const LLCachedControl<std::string> local_chat_date_format(gSavedPerAccountSettings, "LogFileLocalChatDateFormat", "-%Y-%m-%d");
static const LLCachedControl<std::string> ims_date_format(gSavedPerAccountSettings, "LogFileIMsDateFormat", "-%Y-%m");
strftime(dbuffer.data(), dbuffer.size(), (filename == "chat" ? local_chat_date_format : ims_date_format)().c_str(), localtime(&now));
filename += dbuffer.data();
}
cleanFileName(filename);
return get_log_dir_file(filename + ".txt");
}
static LLSD sIDMap;
static std::string get_ids_map_file() { return get_log_dir_file("ids_to_names.json"); }
void LLLogChat::initializeIDMap()
{
const auto map_file = get_ids_map_file();
if (LLFile::isfile(map_file)) // If we've already made this file, load our map from it
{
if (auto&& fstr = llifstream(map_file))
{
LLSDSerialize::fromNotation(sIDMap, fstr, LLSDSerialize::SIZE_UNLIMITED);
fstr.close();
}
}
else if (gCacheName) // Load what we can from name cache to initialize the map file
{
for (const auto& r : gCacheName->getReverseMap()) // For every name id pair
if (LLFile::isfile(makeLogFileNameInternal(r.first))) // If there's a log file for them
sIDMap[r.second.asString()] = r.first; // Add them to the map
if (auto&& fstr = llofstream(map_file))
{
LLSDSerialize::toPrettyNotation(sIDMap, fstr);
fstr.close();
}
}
}
//static
std::string LLLogChat::makeLogFileName(const std::string& username, const LLUUID& id)
{
const auto name = username.empty() ? id.asString() : username; // Fall back on ID if the grid sucks and we have no name
std::string filename = makeLogFileNameInternal(name);
if (id.notNull() && !LLFile::isfile(filename)) // No existing file by this user's current name, check for possible file rename
{
auto& entry = sIDMap[id.asString()];
const bool empty = !entry.size();
if (empty || entry != name) // If we haven't seen this entry yet, or the name is different than we remember
{
const auto migrateFile = [&filename](const std::string& name) {
std::string oldname = makeLogFileNameInternal(name);
if (!LLFile::isfile(oldname)) return false; // An old file by this name doesn't exist
LLFile::rename(oldname, filename); // Move the existing file to the new name
return true; // Report success
};
if (empty) // We didn't see this entry on load
{
// Ideally, we would look up the old names here via server request
// In lieu of that, our reverse cache has old names and new names that we've gained since our initialization of the ID map
for (const auto& r : gCacheName->getReverseMap())
if (r.second == id && migrateFile(r.first))
break;
}
else migrateFile(entry.asStringRef()); // We've seen this entry before, migrate old file if it exists
entry = name; // Update the entry to point to the new name
if (auto&& fstr = llofstream(get_ids_map_file())) // Write back to our map file
{
LLSDSerialize::toPrettyNotation(sIDMap, fstr);
fstr.close();
}
}
}
filename = cleanFileName(filename);
filename = gDirUtilp->getExpandedFilename(LL_PATH_PER_ACCOUNT_CHAT_LOGS,filename);
filename += ".txt";
return filename;
}
std::string LLLogChat::cleanFileName(std::string filename)
void LLLogChat::cleanFileName(std::string& filename)
{
std::string invalidChars = "\"\'\\/?*:<>|[]{}~"; // Cannot match glob or illegal filename chars
S32 position = filename.find_first_of(invalidChars);
@@ -73,7 +138,6 @@ std::string LLLogChat::cleanFileName(std::string filename)
filename[position] = '_';
position = filename.find_first_of(invalidChars, position);
}
return filename;
}
static void time_format(std::string& out, const char* fmt, const std::tm* time)
@@ -92,6 +156,7 @@ static void time_format(std::string& out, const char* fmt, const std::tm* time)
charvector.resize(1+size); // Use the String Stone
format_the_time();
}
#undef format_the_time
out.assign(charvector.data());
}
@@ -117,15 +182,15 @@ std::string LLLogChat::timestamp(bool withdate)
//static
void LLLogChat::saveHistory(std::string const& filename, std::string line)
void LLLogChat::saveHistory(const std::string& name, const LLUUID& id, const std::string& line)
{
if(!filename.size())
if(name.empty() && id.isNull())
{
LL_INFOS() << "Filename is Empty!" << LL_ENDL;
return;
}
LLFILE* fp = LLFile::fopen(LLLogChat::makeLogFileName(filename), "a"); /*Flawfinder: ignore*/
LLFILE* fp = LLFile::fopen(LLLogChat::makeLogFileName(name, id), "a"); /*Flawfinder: ignore*/
if (!fp)
{
LL_INFOS() << "Couldn't open chat history log!" << LL_ENDL;
@@ -140,10 +205,9 @@ void LLLogChat::saveHistory(std::string const& filename, std::string line)
static long const LOG_RECALL_BUFSIZ = 2048;
void LLLogChat::loadHistory(std::string const& filename , void (*callback)(ELogLineType, std::string, void*), void* userdata)
void LLLogChat::loadHistory(const std::string& name, const LLUUID& id, std::function<void (ELogLineType, const std::string&)> callback)
{
bool filename_empty = filename.empty();
if (filename_empty)
if (name.empty() && id.isNull())
{
LL_WARNS() << "filename is empty!" << LL_ENDL;
}
@@ -154,7 +218,7 @@ void LLLogChat::loadHistory(std::string const& filename , void (*callback)(ELogL
if (lines == 0) break;
// Open the log file.
LLFILE* fptr = LLFile::fopen(makeLogFileName(filename), "rb");
LLFILE* fptr = LLFile::fopen(makeLogFileName(name, id), "rb");
if (!fptr) break;
// Set pos to point to the last character of the file, if any.
@@ -201,18 +265,18 @@ void LLLogChat::loadHistory(std::string const& filename , void (*callback)(ELogL
{
size_t len = strlen(buffer);
int i = len - 1;
while (i >= 0 && (buffer[i] == '\r' || buffer[i] == '\n')) // strip newline chars from the end of the string
auto& ch = buffer[i];
while (i >= 0 && (ch == '\r' || ch == '\n')) // strip newline chars from the end of the string
{
buffer[i] = '\0';
i--;
ch = '\0';
--i;
}
callback(LOG_LINE, buffer, userdata);
callback(LOG_LINE, buffer);
}
fclose(fptr);
callback(LOG_END, LLStringUtil::null, userdata);
callback(LOG_END, LLStringUtil::null);
return;
}
callback(LOG_EMPTY, LLStringUtil::null, userdata);
callback(LOG_EMPTY, LLStringUtil::null);
}

View File

@@ -45,14 +45,15 @@ public:
LOG_LINE,
LOG_END
};
static void initializeIDMap();
static std::string timestamp(bool withdate = false);
static std::string makeLogFileName(std::string filename);
static void saveHistory(std::string const& filename, std::string line);
static void loadHistory(std::string const& filename,
void (*callback)(ELogLineType,std::string,void*),
void* userdata);
static std::string makeLogFileName(const std::string& name, const LLUUID& id);
static void saveHistory(const std::string& name, const LLUUID& id, const std::string& line);
static void loadHistory(const std::string& name, const LLUUID& id,
std::function<void (ELogLineType, const std::string&)> callback);
private:
static std::string cleanFileName(std::string filename);
static std::string makeLogFileNameInternal(std::string filename);
static void cleanFileName(std::string& filename);
};
#endif

View File

@@ -340,7 +340,7 @@ static std::string profile_picture_title(const std::string& str) { return "Profi
static void show_partner_help() { LLNotificationsUtil::add("ClickPartnerHelpAvatar", LLSD(), LLSD(), boost::bind(LLPanelAvatarSecondLife::onClickPartnerHelpLoadURL, _1, _2)); }
void show_log_browser(const LLUUID& id, const LFIDBearer::Type& type)
{
void show_log_browser(const std::string& name, const std::string& id);
void show_log_browser(const std::string& name, const LLUUID& id);
std::string name;
if (type == LFIDBearer::AVATAR)
{
@@ -352,7 +352,7 @@ void show_log_browser(const LLUUID& id, const LFIDBearer::Type& type)
{
gCacheName->getGroupName(id, name);
}
show_log_browser(name, id.asString());
show_log_browser(name, id);
}
BOOL LLPanelAvatarSecondLife::postBuild()
{

View File

@@ -1837,6 +1837,7 @@ bool idle_startup()
display_startup();
LLStartUp::initNameCache();
LLLogChat::initializeIDMap(); // Name cache loaded, create a happy mappy
display_startup();
// update the voice settings *after* gCacheName initialization