gpt4 book ai didi

c++ - 将日志记录插入项目中每个函数的脚本?

转载 作者:可可西里 更新时间:2023-11-01 13:28:29 25 4
gpt4 key购买 nike

我继承了一个相当大的代码库,90% 是 C++,我需要快速掌握它。在宽目录树结构中有数百个 .cc 文件。

它相当复杂,并且没有日志记录。为了弄清楚一些主要的子系统是如何工作的,我想在每个函数中插入一个函数调用。

例如,给定一个包含如下内容的 .cc 文件:

void A::foo(int a, int b) {
// ...
}

void A::bar() {
// ...
}

void B::bleh(const string& in) {
// ...
}

我想得到这个:

void A::foo(int a, int b) {
LOG(debug) << "A::foo() called.";
// ...
}

void A::bar() {
LOG(debug) << "A::bar() called.";
// ...
}

void B::bleh(const string& in) {
LOG(debug) << "B::bleh() called.";
// ...
}

这可以通过 python 脚本、CMD 脚本、power shell 脚本等来完成。如果有办法让 VS 做到这一点,那就太好了。什么都行。不必很漂亮,我不会检查任何这些。

此外,它不一定需要获取所有内容。例如。嵌套类、头文件中的实现等。

最佳答案

在 VS 中使用宏添加分析代码有类似的东西,这是代码(这还将所有内容分组在一个“撤消”命令下,并在其自己的输出窗口中列出所有更改)

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics

Public Module Module1

Function GetOutputWindowPane(ByVal Name As String, Optional ByVal show As Boolean = True) As OutputWindowPane
Dim window As Window
Dim outputWindow As OutputWindow
Dim outputWindowPane As OutputWindowPane

window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
If show Then window.Visible = True
outputWindow = window.Object
Try
outputWindowPane = outputWindow.OutputWindowPanes.Item(Name)
Catch e As System.Exception
outputWindowPane = outputWindow.OutputWindowPanes.Add(Name)
End Try
outputWindowPane.Activate()
Return outputWindowPane
End Function

Const ToInsert As String = "/* Inserted text :D */"

Sub AddProfilingToFunction(ByVal func As CodeFunction2)
Dim editPoint As EditPoint2 = func.StartPoint.CreateEditPoint()
While editPoint.GetText(1) <> "{"
editPoint.CharRight()
End While

editPoint.CharRight()
editPoint.InsertNewLine(1)

Dim insertStartLine As Integer = editPoint.Line
Dim insertStartChar As Integer = editPoint.LineCharOffset
editPoint.Insert(ToInsert)

GetOutputWindowPane("Macro Inserted Code").OutputString( _
editPoint.Parent.Parent.FullName & _
"(" & insertStartLine & "," & insertStartChar & _
") : Inserted Code """ & ToInsert & """" & vbCrLf)
End Sub

Sub AddProfilingToProject(ByVal proj As Project)
If Not proj.CodeModel() Is Nothing Then
Dim EventTitle As String = "Add Profiling to project '" & proj.Name & "'"
GetOutputWindowPane("Macro Inserted Code").OutputString("Add Profiling to project '" & proj.Name & "'" & vbCrLf)
DTE.UndoContext.Open(EventTitle)
Try
Dim allNames As String = ""
For i As Integer = 1 To proj.CodeModel().CodeElements.Count()
If proj.CodeModel().CodeElements.Item(i).Kind = vsCMElement.vsCMElementFunction Then
AddProfilingToFunction(proj.CodeModel().CodeElements.Item(i))
End If
Next
Finally
DTE.UndoContext.Close()
End Try
GetOutputWindowPane("Macro Inserted Code").OutputString(vbCrLf)
End If
End Sub

Sub AddProfilingToSolution()
GetOutputWindowPane("Macro Inserted Code").Clear()
If Not DTE.Solution Is Nothing And DTE.Solution.IsOpen() Then
For i As Integer = 1 To DTE.Solution.Projects.Count()
AddProfilingToProject(DTE.Solution.Projects.Item(i))
Next
End If
End Sub

End Module

附言请记住将“Const ToInsert As String = ...”更改为您实际要插入的代码

关于c++ - 将日志记录插入项目中每个函数的脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1442436/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com