blob: f8c72ab405d0c0c8e9aa48732c03d89f921b0a09 [file] [log] [blame]
Christopher Ferris6c619a02019-03-01 17:59:51 -08001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <stdarg.h>
20#include <stdio.h>
Christopher Ferrisff88fb02019-11-04 18:40:00 -080021#include <unistd.h>
Christopher Ferris6c619a02019-03-01 17:59:51 -080022
Josh Gao4956c372019-12-19 16:35:51 -080023#include <platform/bionic/macros.h>
Christopher Ferris6c619a02019-03-01 17:59:51 -080024
25class MallocXmlElem {
26 public:
27 // Name must be valid throughout lifetime of the object.
Christopher Ferrisff88fb02019-11-04 18:40:00 -080028 explicit MallocXmlElem(int fd, const char* name,
29 const char* attr_fmt = nullptr, ...) : fd_(fd), name_(name) {
30 dprintf(fd_, "<%s", name_);
Christopher Ferris6c619a02019-03-01 17:59:51 -080031 if (attr_fmt != nullptr) {
32 va_list args;
33 va_start(args, attr_fmt);
Christopher Ferrisff88fb02019-11-04 18:40:00 -080034 write(fd_, " ", 1);
35 vdprintf(fd_, attr_fmt, args);
Christopher Ferris6c619a02019-03-01 17:59:51 -080036 va_end(args);
37 }
Christopher Ferrisff88fb02019-11-04 18:40:00 -080038 write(fd_, ">", 1);
Christopher Ferris6c619a02019-03-01 17:59:51 -080039 }
40
41 ~MallocXmlElem() noexcept {
Christopher Ferrisff88fb02019-11-04 18:40:00 -080042 dprintf(fd_, "</%s>", name_);
Christopher Ferris6c619a02019-03-01 17:59:51 -080043 }
44
45 void Contents(const char* fmt, ...) {
46 va_list args;
47 va_start(args, fmt);
Christopher Ferrisff88fb02019-11-04 18:40:00 -080048 vdprintf(fd_, fmt, args);
Christopher Ferris6c619a02019-03-01 17:59:51 -080049 va_end(args);
50 }
51
52private:
Christopher Ferrisff88fb02019-11-04 18:40:00 -080053 int fd_;
Christopher Ferris6c619a02019-03-01 17:59:51 -080054 const char* name_;
55
56 BIONIC_DISALLOW_IMPLICIT_CONSTRUCTORS(MallocXmlElem);
57};