xref: /petsc/src/sys/logging/handler/interface/lhreg.c (revision b93211885051a68d5a4e2963b141f71fa7a24c4c)
1 
2 #include <petsc/private/petscimpl.h>
3 #include <petsc/private/loghandlerimpl.h>
4 
5 PetscClassId PETSCLOGHANDLER_CLASSID = 0;
6 
7 PetscFunctionList PetscLogHandlerList               = NULL;
8 PetscBool         PetscLogHandlerRegisterAllCalled  = PETSC_FALSE;
9 PetscBool         PetscLogHandlerPackageInitialized = PETSC_FALSE;
10 
11 PETSC_INTERN PetscErrorCode PetscLogHandlerCreate_Default(PetscLogHandler);
12 PETSC_INTERN PetscErrorCode PetscLogHandlerCreate_Nested(PetscLogHandler);
13 
14 static PetscErrorCode PetscLogHandlerRegisterAll(void)
15 {
16   PetscFunctionBegin;
17   if (PetscLogHandlerRegisterAllCalled) PetscFunctionReturn(PETSC_SUCCESS);
18   PetscLogHandlerRegisterAllCalled = PETSC_TRUE;
19   PetscCall(PetscLogHandlerRegister(PETSC_LOG_HANDLER_DEFAULT, PetscLogHandlerCreate_Default));
20   PetscCall(PetscLogHandlerRegister(PETSC_LOG_HANDLER_NESTED, PetscLogHandlerCreate_Nested));
21   PetscFunctionReturn(PETSC_SUCCESS);
22 }
23 
24 /*@C
25   PetscLogHandlerRegister - Register a new `PetscLogHandler`
26 
27   Not collective
28 
29   Input Parameters:
30 + sname    - The name of a new user-defined creation routine
31 - function - The creation routine
32 
33   Example Usage:
34 .vb
35     PetscLogHandlerRegister("my_profiler", MyPetscLogHandlerCreate);
36 .ve
37 
38   Then, your `PetscLogHandler` type can be chosen with the procedural interface via
39 .vb
40     PetscLogHandlerCreate(MPI_Comm, PetscLogHandler *);
41     PetscLogHandlerSetType(PetscFE, "my_fe");
42 .ve
43 
44   Level: developer
45 
46 .seealso: [](ch_profiling), `PetscLogHandler`, `PetscLogHandlerCreate()`, `PetscLogHandlerSetType()`, `PetscLogHandlerGetType()`
47 @*/
48 PetscErrorCode PetscLogHandlerRegister(const char sname[], PetscErrorCode (*function)(PetscLogHandler))
49 {
50   PetscFunctionBegin;
51   PetscCall(PetscFunctionListAdd(&PetscLogHandlerList, sname, function));
52   PetscFunctionReturn(PETSC_SUCCESS);
53 }
54 
55 /*@C
56   PetscLogHandlerSetType - Set the type of a `PetscLogHandler`
57 
58   Input Parameters:
59 + handler - the `PetscLogHandler`
60 - name    - The kind of log handler
61 
62   Level: developer
63 
64 .seealso: [](ch_profiling), `PetscLogHandler`, `PetscLogHandlerCreate()`, `PetscLogHandlerRegister()`, `PetscLogHandlerGetType()`
65 @*/
66 PetscErrorCode PetscLogHandlerSetType(PetscLogHandler handler, PetscLogHandlerType name)
67 {
68   PetscErrorCode (*r)(PetscLogHandler);
69   PetscBool match;
70 
71   PetscFunctionBegin;
72   PetscValidHeaderSpecific(handler, PETSCLOGHANDLER_CLASSID, 1);
73   PetscCall(PetscObjectTypeCompare((PetscObject)handler, name, &match));
74   if (match) PetscFunctionReturn(PETSC_SUCCESS);
75 
76   PetscCall(PetscLogHandlerRegisterAll());
77   PetscCall(PetscFunctionListFind(PetscLogHandlerList, name, &r));
78   PetscCheck(r, PetscObjectComm((PetscObject)handler), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown PetscLogHandler type: %s", name);
79 
80   PetscTryTypeMethod(handler, destroy);
81   handler->ops->destroy = NULL;
82 
83   PetscCall((*r)(handler));
84   PetscCall(PetscObjectChangeTypeName((PetscObject)handler, name));
85   PetscFunctionReturn(PETSC_SUCCESS);
86 }
87 
88 /*@C
89   PetscLogHandlerGetType - Gets the `PetscLoagHandlerType` (as a string) from the `PetscLogHandler` object.
90 
91   Not collective
92 
93   Input Parameter:
94 . handler - the `PetscLogHandler`
95 
96   Output Parameter:
97 . name - The `PetscLogHandlerType` name
98 
99   Level: developer
100 
101 .seealso: [](ch_profiling), `PetscLogHandler`, `PetscLogHandlerCreate()`, `PetscLogHandlerRegister()`, `PetscLogHandlerSetType()`
102 @*/
103 PetscErrorCode PetscLogHandlerGetType(PetscLogHandler handler, PetscLogHandlerType *name)
104 {
105   PetscFunctionBegin;
106   PetscValidHeaderSpecific(handler, PETSCLOGHANDLER_CLASSID, 1);
107   PetscAssertPointer(name, 2);
108   PetscCall(PetscLogHandlerRegisterAll());
109   PetscCall(PetscObjectGetType((PetscObject)handler, name));
110   PetscFunctionReturn(PETSC_SUCCESS);
111 }
112 
113 static PetscErrorCode PetscLogHandlerFinalizePackage(void)
114 {
115   PetscFunctionBegin;
116   PetscCall(PetscFunctionListDestroy(&PetscLogHandlerList));
117   PetscLogHandlerRegisterAllCalled  = PETSC_FALSE;
118   PetscLogHandlerPackageInitialized = PETSC_FALSE;
119   PetscFunctionReturn(PETSC_SUCCESS);
120 }
121 
122 PetscErrorCode PetscLogHandlerPackageInitialize(void)
123 {
124   PetscFunctionBegin;
125   if (PetscLogHandlerPackageInitialized) PetscFunctionReturn(PETSC_SUCCESS);
126   PetscLogHandlerPackageInitialized = PETSC_TRUE;
127 
128   PetscCall(PetscClassIdRegister("Petsc Log Handler", &PETSCLOGHANDLER_CLASSID));
129   PetscCall(PetscLogHandlerRegisterAll());
130   PetscCall(PetscRegisterFinalize(PetscLogHandlerFinalizePackage));
131   {
132     const PetscClassId classids[] = {PETSCLOGHANDLER_CLASSID};
133 
134     PetscCall(PetscInfoProcessClass("loghandler", PETSC_STATIC_ARRAY_LENGTH(classids), classids));
135   }
136   PetscFunctionReturn(PETSC_SUCCESS);
137 }
138