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

New commits:
commit d8dc138be7e69750d1a346b3b49cecc1201e8d46
Author:     Marco Cecchetti <marco.cecche...@collabora.com>
AuthorDate: Wed Nov 1 22:49:07 2023 +0100
Commit:     Marco Cecchetti <marco.cecche...@collabora.com>
CommitDate: Thu Nov 2 00:18:04 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>

diff --git a/include/LibreOfficeKit/LibreOfficeKitEnums.h 
b/include/LibreOfficeKit/LibreOfficeKitEnums.h
index c17c6e811131..45f41a142563 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 1fd04550a7b3..6df7e0cdc341 100644
--- a/include/sfx2/lokhelper.hxx
+++ b/include/sfx2/lokhelper.hxx
@@ -25,6 +25,38 @@
 #include <string_view>
 #include <sfx2/app.hxx>
 
+#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 << ":"; \
+            lok_detail_stream << SAL_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)
+
+#define LOK_DBG(stream) \
+    LOK_LOG_STREAM("debug", "", stream)
+#else
+#define LOK_INFO(area, stream) \
+    SAL_INFO(area, stream) \
+
+#define LOK_WARN(area, stream) \
+    SAL_WARN(area, stream)
+
+#define LOK_DBG(stream) \
+    SAL_DEBUG(stream)
+#endif
+
 struct SFX2_DLLPUBLIC LokMouseEventData
 {
     int mnType;
@@ -194,6 +226,8 @@ public:
 
     static VclPtr<vcl::Window> getInPlaceDocWindow(SfxViewShell* pViewShell);
 
+    static void notifyLog(const std::ostringstream& stream);
+
 private:
     static int createView(SfxViewFrame* pViewFrame, 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 a6939d0085ed..93f92e21c577 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>
@@ -77,6 +79,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* pViewFrame, ViewShellDocId docId)
@@ -323,6 +327,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;
         }
@@ -736,6 +741,34 @@ void SfxLokHelper::notifyContextChange(const 
css::ui::ContextChangeEventObject&
     pViewShell->libreOfficeKitViewCallback(LOK_CALLBACK_CONTEXT_CHANGED, 
aBuffer.getStr());
 }
 
+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 b7023bf1843b..d4dc941151c6 100644
--- a/sfx2/source/view/viewsh.cxx
+++ b/sfx2/source/view/viewsh.cxx
@@ -476,7 +476,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()
@@ -487,13 +487,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;
@@ -524,7 +524,7 @@ void aboutEvent(std::string msg, const 
accessibility::AccessibleEventObject& aEv
 
             if (xContext.is())
             {
-                SAL_INFO("lok.a11y", msg << ": "
+                LOK_INFO("lok.a11y", msg << ": "
                         << "\n  xNewValue: " << xNewValue.get()
                         << "\n  role: " << xContext->getAccessibleRole()
                         << "\n  name: " << xContext->getAccessibleName()
@@ -537,7 +537,7 @@ void aboutEvent(std::string msg, const 
accessibility::AccessibleEventObject& aEv
     }
     catch( const lang::IndexOutOfBoundsException& )
     {
-        SAL_WARN("lok.a11y", "Focused object has invalid index in parent");
+        LOK_WARN("lok.a11y", "Focused object has invalid index in parent");
     }
 }
 
@@ -937,7 +937,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());
     }
 }
@@ -1350,7 +1350,7 @@ 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: "
+                LOK_INFO("lok.a11y", "LOKDocumentFocusListener::notifyEvent: 
STATE_CHANGED: "
                                      << " New State: " << 
stateSetToString(nState)
                                      << ", Old State: " << 
stateSetToString(nOldState));
 
@@ -1474,7 +1474,7 @@ void LOKDocumentFocusListener::notifyEvent(const 
accessibility::AccessibleEventO
 
                 if (nNewPos >= 0)
                 {
-                    SAL_INFO("lok.a11y", 
"LOKDocumentFocusListener::notifyEvent: CARET_CHANGED: "
+                    LOK_INFO("lok.a11y", 
"LOKDocumentFocusListener::notifyEvent: CARET_CHANGED: "
                                          << "new pos: " << nNewPos << ", 
nOldPos: " << nOldPos);
 
                     uno::Reference<XAccessibleText> 
xAccText(getAccessible(aEvent), uno::UNO_QUERY);
@@ -1509,12 +1509,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);
@@ -1582,7 +1582,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);
@@ -1639,7 +1639,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:
@@ -1648,7 +1648,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");
     }
 }
@@ -1678,7 +1678,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 >();
@@ -1688,7 +1688,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();
@@ -1702,7 +1702,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()
@@ -1726,7 +1726,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()
@@ -1740,12 +1740,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()))
@@ -1830,7 +1830,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());
@@ -3285,6 +3285,7 @@ void 
SfxViewShell::libreOfficeKitViewAddPendingInvalidateTiles()
 
 void SfxViewShell::afterCallbackRegistered()
 {
+    LOK_INFO("sfx.view", "SfxViewShell::afterCallbackRegistered invoked");
     if (GetLOKAccessibilityState())
     {
         LOKDocumentFocusListener& rDocFocusListener = 
GetLOKDocumentFocusListener();
@@ -3375,7 +3376,7 @@ void SfxViewShell::SetLOKAccessibilityState(bool bEnabled)
         }
         catch (const uno::Exception&)
         {
-            SAL_WARN("SetLOKAccessibilityState", "Exception caught processing 
LOKDocumentFocusListener::attachRecursive");
+            LOK_WARN("SetLOKAccessibilityState", "Exception caught processing 
LOKDocumentFocusListener::attachRecursive");
         }
     }
     else
@@ -3386,7 +3387,7 @@ void SfxViewShell::SetLOKAccessibilityState(bool bEnabled)
         }
         catch (const uno::Exception&)
         {
-            SAL_WARN("SetLOKAccessibilityState", "Exception caught processing 
LOKDocumentFocusListener::detachRecursive");
+            LOK_WARN("SetLOKAccessibilityState", "Exception caught processing 
LOKDocumentFocusListener::detachRecursive");
         }
     }
 }

Reply via email to