include/LibreOfficeKit/LibreOfficeKitEnums.h |   12 +++++-
 include/sfx2/lokhelper.hxx                   |   31 ++++++++++++++++
 libreofficekit/source/gtk/lokdocview.cxx     |    1 
 sfx2/source/view/lokhelper.cxx               |   33 +++++++++++++++++
 sfx2/source/view/viewsh.cxx                  |   51 +++++++++++++--------------
 5 files changed, 102 insertions(+), 26 deletions(-)

New commits:
commit 96ed41755b82cd47c27f40d4231e6ed9f915a976
Author:     Marco Cecchetti <marco.cecche...@collabora.com>
AuthorDate: Wed Nov 1 22:49:07 2023 +0100
Commit:     Caolán McNamara <caolan.mcnam...@collabora.com>
CommitDate: Sat Nov 11 22:22:52 2023 +0100

    lok: a11y: implemented support for notifying core log to client
    
    In this way core log can be printed to the browser console.
    This may help in understanding if some core event occurs earlier or
    later wrt a client event.
    
    Change-Id: I720ef9b149e98ddbc252aa069649019e79ef6cb8
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/158780
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com>
    Reviewed-by: Marco Cecchetti <marco.cecche...@collabora.com>
    (cherry picked from commit d8dc138be7e69750d1a346b3b49cecc1201e8d46)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/159331
    Tested-by: Jenkins
    Reviewed-by: Caolán McNamara <caolan.mcnam...@collabora.com>

diff --git a/include/LibreOfficeKit/LibreOfficeKitEnums.h 
b/include/LibreOfficeKit/LibreOfficeKitEnums.h
index 378347108de6..5eb0602e38f5 100644
--- a/include/LibreOfficeKit/LibreOfficeKitEnums.h
+++ b/include/LibreOfficeKit/LibreOfficeKitEnums.h
@@ -1025,7 +1025,15 @@ typedef enum
      *      "text": text content if any
      *  }
      */
-    LOK_CALLBACK_A11Y_SELECTION_CHANGED = 69
+    LOK_CALLBACK_A11Y_SELECTION_CHANGED = 69,
+
+    /**
+     * Forwarding logs from core to client can be useful
+     * for keep track of the real core/client event sequence
+     *
+     * Payload is the log to be sent
+     */
+    LOK_CALLBACK_CORE_LOG = 70
 
 }
 LibreOfficeKitCallbackType;
@@ -1195,6 +1203,8 @@ static inline const char* lokCallbackTypeToString(int 
nType)
         return "LOK_CALLBACK_A11Y_EDITING_IN_SELECTION_STATE";
     case LOK_CALLBACK_A11Y_SELECTION_CHANGED:
         return "LOK_CALLBACK_A11Y_SELECTION_CHANGED";
+    case LOK_CALLBACK_CORE_LOG:
+        return "LOK_CALLBACK_CORE_LOG";
     }
 
     assert(!"Unknown LibreOfficeKitCallbackType type.");
diff --git a/include/sfx2/lokhelper.hxx b/include/sfx2/lokhelper.hxx
index bd2014f9c9d9..f73312c56417 100644
--- a/include/sfx2/lokhelper.hxx
+++ b/include/sfx2/lokhelper.hxx
@@ -26,6 +26,35 @@
 #include <optional>
 #include <string_view>
 
+#define LOK_NOTIFY_LOG_TO_CLIENT 1
+
+#define LOK_LOG_STREAM(level, area, stream) \
+    do { \
+            ::std::ostringstream lok_detail_stream; \
+            lok_detail_stream << level << ':'; \
+            if (std::strcmp(level, "debug") != 0) \
+                lok_detail_stream << area << ':'; \
+            const char* const where = SAL_WHERE; \
+            lok_detail_stream << where << stream; \
+            SfxLokHelper::notifyLog(lok_detail_stream); \
+        } while (false)
+
+#if LOK_NOTIFY_LOG_TO_CLIENT > 0
+#define LOK_INFO(area, stream) \
+    LOK_LOG_STREAM("info", area, stream) \
+
+#define LOK_WARN(area, stream) \
+    LOK_LOG_STREAM("warn", area, stream)
+
+#else
+#define LOK_INFO(area, stream) \
+    SAL_INFO(area, stream) \
+
+#define LOK_WARN(area, stream) \
+    SAL_WARN(area, stream)
+
+#endif
+
 struct SFX2_DLLPUBLIC LokMouseEventData
 {
     int mnType;
@@ -195,6 +224,8 @@ public:
 
     static VclPtr<vcl::Window> getInPlaceDocWindow(SfxViewShell* pViewShell);
 
+    static void notifyLog(const std::ostringstream& stream);
+
 private:
     static int createView(SfxViewFrame& rViewFrame, ViewShellDocId docId);
 };
diff --git a/libreofficekit/source/gtk/lokdocview.cxx 
b/libreofficekit/source/gtk/lokdocview.cxx
index 6c7e6dbfc652..bd2cec88f071 100644
--- a/libreofficekit/source/gtk/lokdocview.cxx
+++ b/libreofficekit/source/gtk/lokdocview.cxx
@@ -1496,6 +1496,7 @@ callback (gpointer pData)
     case LOK_CALLBACK_DOCUMENT_PASSWORD_RESET:
     case LOK_CALLBACK_A11Y_EDITING_IN_SELECTION_STATE:
     case LOK_CALLBACK_A11Y_SELECTION_CHANGED:
+    case LOK_CALLBACK_CORE_LOG:
     {
         // TODO: Implement me
         break;
diff --git a/sfx2/source/view/lokhelper.cxx b/sfx2/source/view/lokhelper.cxx
index e7fdc3d024be..31981159ca02 100644
--- a/sfx2/source/view/lokhelper.cxx
+++ b/sfx2/source/view/lokhelper.cxx
@@ -9,7 +9,9 @@
 
 #include <sal/config.h>
 
+#include <string>
 #include <string_view>
+#include <list>
 
 #include <sfx2/lokcomponenthelpers.hxx>
 #include <sfx2/lokhelper.hxx>
@@ -79,6 +81,8 @@ LanguageTag g_loadLanguageTag("en-US", true); //< The 
language used to load.
 LOKDeviceFormFactor g_deviceFormFactor = LOKDeviceFormFactor::UNKNOWN;
 bool g_isDefaultTimezoneSet = false;
 OUString g_DefaultTimezone;
+const std::size_t g_logNotifierCacheMaxSize = 50;
+::std::list<::std::string> g_logNotifierCache;
 }
 
 int SfxLokHelper::createView(SfxViewFrame& rViewFrame, ViewShellDocId docId)
@@ -322,6 +326,7 @@ void SfxLokHelper::setAccessibilityState(int nId, bool 
nEnabled)
     {
         if (pViewShell->GetViewShellId() == ViewShellId(nId))
         {
+            LOK_INFO("lok.a11y", "SfxLokHelper::setAccessibilityState: view 
id: " << nId << ", nEnabled: " << nEnabled);
             pViewShell->SetLOKAccessibilityState(nEnabled);
             return;
         }
@@ -732,6 +737,34 @@ void SfxLokHelper::notifyContextChange(const 
css::ui::ContextChangeEventObject&
     pViewShell->libreOfficeKitViewCallback(LOK_CALLBACK_CONTEXT_CHANGED, 
aBuffer.toUtf8());
 }
 
+void SfxLokHelper::notifyLog(const std::ostringstream& stream)
+{
+    if (DisableCallbacks::disabled())
+       return;
+
+    SfxViewShell* pViewShell = SfxViewShell::Current();
+    if (!pViewShell)
+       return;
+    if (pViewShell->getLibreOfficeKitViewCallback())
+    {
+        if (!g_logNotifierCache.empty())
+        {
+            for (const auto& msg : g_logNotifierCache)
+            {
+                pViewShell->libreOfficeKitViewCallback(LOK_CALLBACK_CORE_LOG, 
msg.c_str());
+            }
+            g_logNotifierCache.clear();
+        }
+        pViewShell->libreOfficeKitViewCallback(LOK_CALLBACK_CORE_LOG, 
stream.str().c_str());
+    }
+    else
+    {
+        while (g_logNotifierCache.size() >= g_logNotifierCacheMaxSize)
+            g_logNotifierCache.pop_front();
+        g_logNotifierCache.push_back(stream.str());
+    }
+}
+
 void SfxLokHelper::notifyUpdate(SfxViewShell const* pThisView, int nType)
 {
     if (DisableCallbacks::disabled())
diff --git a/sfx2/source/view/viewsh.cxx b/sfx2/source/view/viewsh.cxx
index 8f2218c66175..6c5377a3bed2 100644
--- a/sfx2/source/view/viewsh.cxx
+++ b/sfx2/source/view/viewsh.cxx
@@ -480,7 +480,7 @@ void aboutEvent(std::string msg, const 
accessibility::AccessibleEventObject& aEv
 
             if (xContext.is())
             {
-                SAL_INFO("lok.a11y", msg << ": event id: " << aEvent.EventId
+                LOK_INFO("lok.a11y", msg << ": event id: " << aEvent.EventId
                         << "\n  xSource: " << xSource.get()
                         << "\n  role: " << xContext->getAccessibleRole()
                         << "\n  name: " << xContext->getAccessibleName()
@@ -491,13 +491,13 @@ void aboutEvent(std::string msg, const 
accessibility::AccessibleEventObject& aEv
             }
             else
             {
-                SAL_INFO("lok.a11y", msg << ": event id: " << aEvent.EventId
+                LOK_INFO("lok.a11y", msg << ": event id: " << aEvent.EventId
                                          << ", no accessible context!");
             }
         }
         else
         {
-            SAL_INFO("lok.a11y", msg << ": event id: " << aEvent.EventId
+            LOK_INFO("lok.a11y", msg << ": event id: " << aEvent.EventId
                                      << ", no accessible source!");
         }
         uno::Reference< accessibility::XAccessible > xOldValue;
@@ -528,8 +528,8 @@ void aboutEvent(std::string msg, const 
accessibility::AccessibleEventObject& aEv
 
             if (xContext.is())
             {
-                SAL_INFO("lok.a11y", msg << ": "
-                        "\n  xNewValue: " << xNewValue.get()
+                LOK_INFO("lok.a11y", msg << ": "
+                           "\n  xNewValue: " << xNewValue.get()
                         << "\n  role: " << xContext->getAccessibleRole()
                         << "\n  name: " << xContext->getAccessibleName()
                         << "\n  index in parent: " << 
xContext->getAccessibleIndexInParent()
@@ -541,7 +541,7 @@ void aboutEvent(std::string msg, const 
accessibility::AccessibleEventObject& aEv
     }
     catch( const lang::IndexOutOfBoundsException& /*e*/ )
     {
-        SAL_WARN("lok.a11y", "Focused object has invalid index in parent");
+        LOK_WARN("lok.a11y", "Focused object has invalid index in parent");
     }
 }
 
@@ -940,7 +940,7 @@ void 
LOKDocumentFocusListener::notifyEditingInSelectionState(bool bParagraph)
     std::string aPayload = aStream.str();
     if (m_pViewShell)
     {
-        SAL_INFO("lok.a11y", 
"LOKDocumentFocusListener::notifyEditingInSelectionState: payload: \n" << 
aPayload);
+        LOK_INFO("lok.a11y", 
"LOKDocumentFocusListener::notifyEditingInSelectionState: payload: \n" << 
aPayload);
         
m_pViewShell->libreOfficeKitViewCallback(LOK_CALLBACK_A11Y_EDITING_IN_SELECTION_STATE,
 aPayload.c_str());
     }
 }
@@ -1352,8 +1352,8 @@ void LOKDocumentFocusListener::notifyEvent(const 
accessibility::AccessibleEventO
                 aEvent.NewValue >>= nState;
                 sal_Int64 nOldState = 
accessibility::AccessibleStateType::INVALID;
                 aEvent.OldValue >>= nOldState;
-                SAL_INFO("lok.a11y", "LOKDocumentFocusListener::notifyEvent: 
STATE_CHANGED: "
-                                     " New State: " << stateSetToString(nState)
+                LOK_INFO("lok.a11y", "LOKDocumentFocusListener::notifyEvent: 
STATE_CHANGED: "
+                                        " New State: " << 
stateSetToString(nState)
                                      << ", Old State: " << 
stateSetToString(nOldState));
 
                 // check validity
@@ -1476,8 +1476,8 @@ void LOKDocumentFocusListener::notifyEvent(const 
accessibility::AccessibleEventO
 
                 if (nNewPos >= 0)
                 {
-                    SAL_INFO("lok.a11y", 
"LOKDocumentFocusListener::notifyEvent: CARET_CHANGED: "
-                                         "new pos: " << nNewPos << ", nOldPos: 
" << nOldPos);
+                    LOK_INFO("lok.a11y", 
"LOKDocumentFocusListener::notifyEvent: CARET_CHANGED: "
+                                            "new pos: " << nNewPos << ", 
nOldPos: " << nOldPos);
 
                     uno::Reference<XAccessibleText> 
xAccText(getAccessible(aEvent), uno::UNO_QUERY);
                     if (xAccText.is())
@@ -1511,12 +1511,12 @@ void LOKDocumentFocusListener::notifyEvent(const 
accessibility::AccessibleEventO
 
                 if (aEvent.OldValue >>= aDeletedText)
                 {
-                    SAL_INFO("lok.a11y", 
"LOKDocumentFocusListener::notifyEvent: TEXT_CHANGED: "
+                    LOK_INFO("lok.a11y", 
"LOKDocumentFocusListener::notifyEvent: TEXT_CHANGED: "
                                              "deleted text: >" << 
aDeletedText.SegmentText << "<");
                 }
                 if (aEvent.NewValue >>= aInsertedText)
                 {
-                    SAL_INFO("lok.a11y", 
"LOKDocumentFocusListener::notifyEvent: TEXT_CHANGED: "
+                    LOK_INFO("lok.a11y", 
"LOKDocumentFocusListener::notifyEvent: TEXT_CHANGED: "
                                              "inserted text: >" << 
aInsertedText.SegmentText << "<");
                 }
                 uno::Reference<XAccessibleText> 
xAccText(getAccessible(aEvent), uno::UNO_QUERY);
@@ -1584,7 +1584,7 @@ void LOKDocumentFocusListener::notifyEvent(const 
accessibility::AccessibleEventO
                         return; // selecting the same object; note: on editing 
selected object is cleared
                 else
                     m_xSelectedObject = xSelectedObject;
-                SAL_INFO("lok.a11y", "LOKDocumentFocusListener::notifyEvent: 
SELECTION_CHANGED: "
+                LOK_INFO("lok.a11y", "LOKDocumentFocusListener::notifyEvent: 
SELECTION_CHANGED: "
                                          "m_xSelectedObject.is(): " << 
m_xSelectedObject.is());
 
                 OUString sAction = selectionEventTypeToString(aEvent.EventId);
@@ -1641,7 +1641,7 @@ void LOKDocumentFocusListener::notifyEvent(const 
accessibility::AccessibleEventO
             }
             case AccessibleEventId::INVALIDATE_ALL_CHILDREN:
             {
-                SAL_INFO("lok.a11y", "Invalidate all children called");
+                LOK_INFO("lok.a11y", "Invalidate all children called");
                 break;
             }
             default:
@@ -1650,7 +1650,7 @@ void LOKDocumentFocusListener::notifyEvent(const 
accessibility::AccessibleEventO
     }
     catch( const lang::IndexOutOfBoundsException& )
     {
-        SAL_WARN("lok.a11y",
+        LOK_WARN("lok.a11y",
                  "LOKDocumentFocusListener::notifyEvent:Focused object has 
invalid index in parent");
     }
 }
@@ -1680,7 +1680,7 @@ uno::Reference< accessibility::XAccessible > 
LOKDocumentFocusListener::getAccess
         }
     }
 
-    SAL_WARN("lok.a11y",
+    LOK_WARN("lok.a11y",
              "LOKDocumentFocusListener::getAccessible: Can't get any 
accessible object from event source.");
 
     return uno::Reference< accessibility::XAccessible >();
@@ -1690,7 +1690,7 @@ void LOKDocumentFocusListener::attachRecursive(
     const uno::Reference< accessibility::XAccessible >& xAccessible
 )
 {
-    SAL_INFO("lok.a11y", "LOKDocumentFocusListener::attachRecursive(1): 
xAccessible: " << xAccessible.get());
+    LOK_INFO("lok.a11y", "LOKDocumentFocusListener::attachRecursive(1): 
xAccessible: " << xAccessible.get());
 
     uno::Reference< accessibility::XAccessibleContext > xContext =
         xAccessible->getAccessibleContext();
@@ -1704,7 +1704,7 @@ void LOKDocumentFocusListener::attachRecursive(
     const uno::Reference< accessibility::XAccessibleContext >& xContext
 )
 {
-    SAL_INFO("lok.a11y", "LOKDocumentFocusListener::attachRecursive(2): 
xAccessible: " << xAccessible.get()
+    LOK_INFO("lok.a11y", "LOKDocumentFocusListener::attachRecursive(2): 
xAccessible: " << xAccessible.get()
             << ", role: " << xContext->getAccessibleRole()
             << ", name: " << xContext->getAccessibleName()
             << ", parent: " << xContext->getAccessibleParent().get()
@@ -1728,7 +1728,7 @@ void LOKDocumentFocusListener::attachRecursive(
 )
 {
     aboutView("LOKDocumentFocusListener::attachRecursive (3)", this, 
m_pViewShell);
-    SAL_INFO("lok.a11y", "LOKDocumentFocusListener::attachRecursive(3) #1: 
this: " << this
+    LOK_INFO("lok.a11y", "LOKDocumentFocusListener::attachRecursive(3) #1: 
this: " << this
             << ", xAccessible: " << xAccessible.get()
             << ", role: " << xContext->getAccessibleRole()
             << ", name: " << xContext->getAccessibleName()
@@ -1741,12 +1741,12 @@ void LOKDocumentFocusListener::attachRecursive(
 
     if (!xBroadcaster.is())
         return;
-    SAL_INFO("lok.a11y", "LOKDocumentFocusListener::attachRecursive(3) #2: 
xBroadcaster.is()");
+    LOK_INFO("lok.a11y", "LOKDocumentFocusListener::attachRecursive(3) #2: 
xBroadcaster.is()");
     // If not already done, add the broadcaster to the list and attach as 
listener.
     const uno::Reference< uno::XInterface >& xInterface = xBroadcaster;
     if( m_aRefList.insert(xInterface).second )
     {
-        SAL_INFO("lok.a11y", "LOKDocumentFocusListener::attachRecursive(3) #3: 
m_aRefList.insert(xInterface).second");
+        LOK_INFO("lok.a11y", "LOKDocumentFocusListener::attachRecursive(3) #3: 
m_aRefList.insert(xInterface).second");
         xBroadcaster->addAccessibleEventListener(static_cast< 
accessibility::XAccessibleEventListener *>(this));
 
         if (isDocument(xContext->getAccessibleRole()))
@@ -1831,7 +1831,7 @@ void LOKDocumentFocusListener::detachRecursive(
     aboutView("LOKDocumentFocusListener::detachRecursive (2)", this, 
m_pViewShell);
     sal_Int64 nStateSet = xContext->getAccessibleStateSet();
 
-    SAL_INFO("lok.a11y", "LOKDocumentFocusListener::detachRecursive(2): this: 
" << this
+    LOK_INFO("lok.a11y", "LOKDocumentFocusListener::detachRecursive(2): this: 
" << this
             << ", name: " << xContext->getAccessibleName()
             << ", parent: " << xContext->getAccessibleParent().get()
             << ", child count: " << xContext->getAccessibleChildCount());
@@ -3283,6 +3283,7 @@ void 
SfxViewShell::libreOfficeKitViewAddPendingInvalidateTiles()
 
 void SfxViewShell::afterCallbackRegistered()
 {
+    LOK_INFO("sfx.view", "SfxViewShell::afterCallbackRegistered invoked");
     if (GetLOKAccessibilityState())
     {
         LOKDocumentFocusListener& rDocFocusListener = 
GetLOKDocumentFocusListener();
@@ -3373,7 +3374,7 @@ void SfxViewShell::SetLOKAccessibilityState(bool bEnabled)
         }
         catch (const uno::Exception&)
         {
-            SAL_WARN("lok.a11y", "Exception caught processing 
LOKDocumentFocusListener::attachRecursive");
+            LOK_WARN("SetLOKAccessibilityState", "Exception caught processing 
LOKDocumentFocusListener::attachRecursive");
         }
     }
     else
@@ -3384,7 +3385,7 @@ void SfxViewShell::SetLOKAccessibilityState(bool bEnabled)
         }
         catch (const uno::Exception&)
         {
-            SAL_WARN("lok.a11y", "Exception caught processing 
LOKDocumentFocusListener::detachRecursive");
+            LOK_WARN("SetLOKAccessibilityState", "Exception caught processing 
LOKDocumentFocusListener::detachRecursive");
         }
     }
 }

Reply via email to